问题
Using Xcode 7.1
In the Alamofire responseJSON request I cannot put 4 parameters. Below is the code
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET, url2).responseJSON{ request, response, JSON, error in
print(JSON)
}
I get this error: Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result)' (aka '(Optional, Optional, Result)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)
If I remove the "error" parameter from responseJSON and run it...the app builds but no json is printed on the console..
let url2 = "https://httpbin.org/get"
Alamofire.request(.GET, url2).responseJSON{ request, response, JSON in
print(JSON)
}
Console Output
There is no JSON being printed. If you go to sample URL from the code you will see JSON.
I have followed the instructions from GitHub but its not working
回答1:
Alamofire v1.x had four parameters to the responseJSON
closure. Alamofire v2.x had three parameters. Alamofire v3.x now calls the closure with a single parameter, 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)
}
}
Alternatively, you can use the isSuccess
, isFailure
, value
, data
and error
computed properties for Result
, e.g.:
Alamofire.request(.GET, url2).responseJSON { response in
print(response.result.value)
}
[This has been updated for the Alamofire 3 syntax. If you need the Alamofire 2 syntax, please refer to this question's revision history.]
回答2:
The documentation on Github has not been updated to the latest version of Alamofire.
To see the properties that Rob pointed out, check the source code of the Framework.
来源:https://stackoverflow.com/questions/32506703/alamofire-not-working-properly-tuple-issue-different-number-of-elements