【问题标题】:Alamofire impossible to create throw inside .responseJSONAlamofire 无法在 .responseJSON 中创建 throw
【发布时间】:2015-09-11 19:06:21
【问题描述】:

我有这种方法可以联系我的 REST 网络服务。 我想在连接失败时生成异常。 Xcode 告诉我不能使用函数 throw VendingMachineError.InvalidSelection 因为 method .responseJSON {(request, response, JSON) in ecc ... 无法进行转换。 我怎样才能解决这个问题?谢谢你的帮助。

【问题讨论】:

  • 如果你已经解决了,那就把你的答案作为答案,这样可能对其他人有帮助。

标签: swift2 alamofire


【解决方案1】:

已解决 我通过输入错误解决了这个问题

do {
    throw NSError(domain: "howdy", code: 1, userInfo:nil)
} catch let error as NSError {
    print( "entrato nel catch primo \(error)")
}

这样我的错误可以被其他方法继承。完整代码:

Alamofire.request(.POST, "http://localhost:8080/progetto/login", headers: headers)
          ///  .validate(statusCode: 200..<200)
            .validate(contentType: ["application/json"])

            .responseJSON {(request,response, JSON) in

                switch JSON{
                case .Success(let data):
                    let result = data as! NSDictionary
                    let codeResponse = response?.statusCode
                    completionHandler(elements: result,stausCode: codeResponse!)
                    print("ciao sono entrato")

                case .Failure( _, let error):
                    print("sono entrato nel fallimento \(error)")
                    do {
                        throw NSError(domain: "howdy", code: 1, userInfo:nil)
                    } catch let error as NSError {
                        print( "entrato nel catch primo \(error)")
                    }
                }

            }

    }

【讨论】:

  • 在这种情况下,错误仍然不会出现在父亲的方法中(login() throws
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多