我正在等待 Alamofire 工程师的指导,但我想看看在此期间我能完成什么.enum 路由器:URLRequestConvertible {静态 let baseURLString = "http://example.com"静态变量 OAuthToken:字符串?案例 CreateUser([String: AnyObject])案例读取用户(字符串)案例更新用户(字符串,[字符串:AnyObject])案例销毁用户(字符串)var 方法:Alamofire.Method {切换自我{案例.CreateUser:返回.POST案例.ReadUser:返回 .GET案例.UpdateUser:返回.PUT案例.DestroyUser:返回 .DELETE}}变量路径:字符串{切换自我{案例.CreateUser:返回/用户"case .ReadUser(让用户名):返回/用户/(用户名)"case .UpdateUser(让用户名,_):返回/用户/(用户名)"case .DestroyUser(让用户名):返回/用户/(用户名)"}}//MARK: URLRequestConvertiblevar URLRequest: NSMutableURLRequest {让 URL = NSURL(string: Router.baseURLString)!让 mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))mutableURLRequest.HTTPMethod = method.rawValue如果让令牌 = Router.OAuthToken {mutableURLRequest.setValue("Bearer (token)", forHTTPHeaderField: "Authorization")}切换自我{案例.CreateUser(让参数):返回 Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0case .UpdateUser(_, let 参数):返回 Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0默认:返回 mutableURLRequest}}} 解决方案 针对 Alamofire 4.0.0 版本进行了编辑(更新了具有抛出功能的 URLRequestConvertible 协议):Swift 3 中的很多发生了变化,您应该首先真正阅读所有变化,也许从 http://swift.org.这是固定代码:enum 路由器:URLRequestConvertible {static let baseURLString = "http://example.com";静态变量 OAuthToken:字符串?case createUser([String: AnyObject])案例读取用户(字符串)case updateUser(String, [String: AnyObject])案例销毁用户(字符串)var 方法:Alamofire.HTTPMethod {切换自我{案例.createUser:返回.post案例.readUser:返回 .get案例.updateUser:返回 .put案例.destroyUser:返回 .delete}}变量路径:字符串{切换自我{案例.createUser:返回/用户"案例.readUser(让用户名):返回/用户/(用户名)";case .updateUser(让用户名,_):返回/用户/(用户名)";案例.destroyUser(让用户名):返回/用户/(用户名)";}}func asURLRequest() 抛出 ->网址请求{让 url = URL(string: Router.baseURLString)!var urlRequest = URLRequest(url: url.appendingPathComponent(path))urlRequest.httpMethod = method.rawValue如果让令牌 = Router.OAuthToken {urlRequest.setValue("Bearer (token)", forHTTPHeaderField: "Authorization")}切换自我{案例.createUser(让参数):返回尝试 Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters)case .updateUser(_, let 参数):返回尝试 Alamofire.URLEncoding.default.encode(urlRequest, with: parameters)默认:返回网址请求}}}Swift 3 的主要变化是:enum case 现在是小写的,你也应该采用它.变量名现在以小写开头,即使它是URL"这样的缩写.这就是为什么协议需要 var urlRequest 而不是 var URLRequest(这会与下一点冲突)Bye-bye NS 前缀在很多地方.NSURLRequest 和 NSMutableURLRequest 现在是 URLRequest,NSURL 是 URL,等等莉>您现在命名函数和参数的方式不再多余,而且更加自然.例如,查看 URLByAppendingPathComponent 的变化情况.至于 Alamofire v4 :有一个新的 ParameterEncoding 协议来编码参数自己是不同的,但更通用许多其他更改在这里没有影响,但您肯定也必须阅读它们.最后一句忠告:避免迁移到时间敏感的编程语言或 API 的未发布版本.Swift 3 不会有太大变化,但 Alamofire 仍然可能!例如,ParameterEncoding 协议只有两天的历史!(实际上它发生了变化,现在是上面的最终版本)干杯Does anybody know how to change this entire approach to Swift 3? At this moment I have something very similar to this working OK on Swift 2.2 but now I'm trying to change that to Swift 3.I am getting some errors with the "URLRequestConvertible", with the Alamofire.Method (that I changed to HTTPMethod and now is working) and also with the parameter encoding, besides that I'm not conforming the entire protocol.I'm waiting for guidance from engineers at Alamofire, but I am looking to see what I can accomplish in the meantime.enum Router: URLRequestConvertible {static let baseURLString = "http://example.com"static var OAuthToken: String?case CreateUser([String: AnyObject])case ReadUser(String)case UpdateUser(String, [String: AnyObject])case DestroyUser(String)var method: Alamofire.Method { switch self { case .CreateUser: return .POST case .ReadUser: return .GET case .UpdateUser: return .PUT case .DestroyUser: return .DELETE }}var path: String { switch self { case .CreateUser: return "/users" case .ReadUser(let username): return "/users/(username)" case .UpdateUser(let username, _): return "/users/(username)" case .DestroyUser(let username): return "/users/(username)" }}// MARK: URLRequestConvertiblevar URLRequest: NSMutableURLRequest { let URL = NSURL(string: Router.baseURLString)! let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path)) mutableURLRequest.HTTPMethod = method.rawValue if let token = Router.OAuthToken { mutableURLRequest.setValue("Bearer (token)", forHTTPHeaderField: "Authorization") } switch self { case .CreateUser(let parameters): return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0 case .UpdateUser(_, let parameters): return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0 default: return mutableURLRequest }}} 解决方案 EDITED for Alamofire 4.0.0 release (updated URLRequestConvertible protocol with throwing capabilities):A lot has changed in Swift 3 and you should first really read up on all the changes, maybe starting at http://swift.org. Here's the fixed code:enum Router: URLRequestConvertible { static let baseURLString = "http://example.com" static var OAuthToken: String? case createUser([String: AnyObject]) case readUser(String) case updateUser(String, [String: AnyObject]) case destroyUser(String) var method: Alamofire.HTTPMethod { switch self { case .createUser: return .post case .readUser: return .get case .updateUser: return .put case .destroyUser: return .delete } } var path: String { switch self { case .createUser: return "/users" case .readUser(let username): return "/users/(username)" case .updateUser(let username, _): return "/users/(username)" case .destroyUser(let username): return "/users/(username)" } } func asURLRequest() throws -> URLRequest { let url = URL(string: Router.baseURLString)! var urlRequest = URLRequest(url: url.appendingPathComponent(path)) urlRequest.httpMethod = method.rawValue if let token = Router.OAuthToken { urlRequest.setValue("Bearer (token)", forHTTPHeaderField: "Authorization") } switch self { case .createUser(let parameters): return try Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters) case .updateUser(_, let parameters): return try Alamofire.URLEncoding.default.encode(urlRequest, with: parameters) default: return urlRequest } }}The main changes for Swift 3 are :enum cases are now lowercase and you should adopt it too.Variable names now start with lowercase, even if it's an abbreviation like "URL". That why the protocol requires var urlRequest and not var URLRequest (and it would conflict with the next point)Bye-bye NS prefix in many places. NSURLRequest and NSMutableURLRequest are now URLRequest, NSURL is URL, etc.How you name your functions and parameters is now a lot less redundant and more natural. See for example how URLByAppendingPathComponent has changed.And as for Alamofire v4 :There's a new ParameterEncoding protocol to encoding parameters yourself is different but more versatileMANY other changes which have no impact here but you sure have to read about them too.And final word of advice : avoid migrating to unreleased versions of a programming language or API if it's time-sensitive. Swift 3 won't budge much but Alamofire still might! For example the ParameterEncoding protocol is only two days old! (EDIT: and indeed it changed since, now in its final version above)Cheers 这篇关于如何将 Alamofire 路由器类迁移到 Swift 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 23:23