使用
Xcode 7.1
在Alamofire responseJSON请求中我不能放4个参数.
下面是代码
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET,url2).responseJSON{ request,response,JSON,error in
print(JSON)
}
我收到此错误:元组类型'(NSURLRequest?,NSHTTPURLResponse?,Result)'(又名'(可选,可选,结果)’)和'(_,_,_)’具有不同数量的元素(3与4)
如果我从responseJSON中删除“error”参数并运行它…应用程序构建但控制台上没有打印json ..
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET,JSON in
print(JSON)
}
控制台输出
没有打印JSON.如果您从代码中获取样本URL,您将看到JSON.
我按照GitHub的说明进行操作,但是没有用
解决方法
Alamofire v1.x有四个参数用于responseJSON闭包. Alamofire v2.x有三个参数. Alamofire v3.x现在使用单个参数调用闭包,响应:
Alamofire.request(.GET,url2).responseJSON { response in
switch (response.result) {
case .Success(let value):
print(value)
case .Failure(let error):
if let data = response.data,let string = String(data: data,encoding: NSUTF8StringEncoding) {
print(string)
}
print(error)
}
}
或者,您可以使用结果的isSuccess,isFailure,value,data和error computed属性,例如:
Alamofire.request(.GET,url2).responseJSON { response in
print(response.result.value)
}
[此已针对Alamofire 3语法进行了更新.如果您需要Alamofire 2语法,请参阅此问题的revision history.]