Anu*_*ora 6 xcode tuples ios alamofire
使用Xcode 7.1
在Alamofire responseJSON请求中我不能放4个参数.下面是代码
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET, url2).responseJSON{ request, response, JSON, error in
print(JSON)
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:元组类型'(NSURLRequest?,NSHTTPURLResponse?,Result)'(又名'(可选,可选,结果)')和'(_,_,_,_)'具有不同数量的元素(3与4)
如果我从responseJSON中删除" error "参数并运行它...应用程序构建但控制台上没有打印json ..
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET, url2).responseJSON{ request, response, JSON in
print(JSON)
}
Run Code Online (Sandbox Code Playgroud)
控制台输出
没有打印JSON.如果您从代码中获取示例URL,您将看到JSON.
我已经按照GitHub的说明进行操作但不起作用
Rob*_*Rob 10
Alamofire v1.x有四个responseJSON关闭参数.Alamofire v2.x有三个参数.Alamofire v3.x现在使用单个参数调用闭包,a Response:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
另外,您也可以使用isSuccess,isFailure,value,data和error计算的属性Result,如:
Alamofire.request(.GET, url2).responseJSON { response in
print(response.result.value)
}
Run Code Online (Sandbox Code Playgroud)
[此已针对Alamofire 3语法进行了更新.如果您需要Alamofire 2语法,请参阅此问题的修订历史.]