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
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.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With