有人知道如何将整个方法更改为Swift 3吗?目前,我有一些与Swift 2.2上的OK正常工作类似的东西,但现在我正尝试将其更改为Swift 3。
除了“ URLRequestConvertible”,Alamofire.Method(我更改为HTTPMethod,现在可以使用)和参数编码之外,我还出现了一些错误,此外我没有遵守整个协议。
我正在等待Alamofire工程师的指导,但我希望在此期间能完成哪些工作。
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: URLRequestConvertible var 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 } } }
URLRequestConvertible
一个 很多 已经在斯威夫特3改变,你应该先认真阅读了所有的变化,也许开始http://swift.org。这是固定代码:
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 } } }
Swift 3的主要更改是:
enum
var urlRequest
var URLRequest
NS
NSURLRequest
NSMutableURLRequest
URLRequest
NSURL
URL
URLByAppendingPathComponent
至于Alamofire v4:
ParameterEncoding
最后的建议是:如果时间敏感,请避免迁移到未发布的编程语言或API版本。Swift 3不会花太多钱,但Alamofire仍然可以!例如,ParameterEncoding协议只有两天了!(编辑:实际上,此后它已经更改,现在在上面的最终版本中)
干杯