【问题标题】:My Router doesn't work after migration of Swift and update of Alamofire迁移 Swift 和更新 Alamofire 后,我的路由器无法工作
【发布时间】:2015-09-28 15:28:20
【问题描述】:

我正在 Swift 2.0 上迁移我的应用程序。我也借此机会将 Alamofire 从 1.3 迁移到 2.0。

但是现在我的应用程序不再工作了,而且我在使用 Alamofire 的文件上出现了很多错误。

首先在我的枚举路由器声明中,我得到一个错误提示

private enum Router: URLRequestConvertible {

编辑:这是实现方法的开始

URLRequestConvertible

    // MARK: URLRequestConvertible

    var URLRequest: NSURLRequest {
        let URL = Router.baseURL.URLByAppendingPathComponent(self.path)
        let URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.HTTPMethod = self.method.rawValue

        switch self {
        case .Login(let email, let password):
            return self.encoding.encode(URLRequest, parameters: [
                "email": email,
                "password": password]).0

        case .Logout:
            return self.encoding.encode(URLRequest, parameters: nil).0
}

类型“Router”不符合协议“URLRequestConvertible”

当我要检查 .responseJSON { (_, _, json, error) in 时,我的所有请求中的第二个我收到了一个错误提示

元组类型 '(NSURLRequest?, NSHTTPURLResponse?, Result)' (又名 '(Optional, Optional, Result)') 和 '(_, _, _, _)' 有不同的 元素数量(3 对 4)

编辑:好的,不再error field,但是你是如何处理错误的呢?

EDIT2:好的,现在您需要为结果使用开关。谢谢

以前没有的错误

感谢您的帮助!

【问题讨论】:

  • 发布你的Router类代码,只发布实现URLRequestConvertible协议的方法。

标签: ios swift swift2 alamofire


【解决方案1】:

第二个错误请参考Alamofire页面https://github.com/Alamofire/Alamofire

如您所见,他们更改了 .responseJSON,现在只返回 3 个参数。在 1.3 版中,有 4 个参数。基本上你只需要以这种方式删除error 字段

.responseJSON { (_, _, json) in

【讨论】:

    【解决方案2】:

    如果您还没有更改,我认为您需要返回“NSMutableURLRequest”而不是“NSURLRequest”。

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2016-07-09
      • 2018-02-05
      • 1970-01-01
      相关资源
      最近更新 更多