I upgraded to Xcode 7 and I am using Alamofire to manage API calls and I am getting this error:
'Contextual type for closure argument list expects 1 argument, but 4 were specified'
For the following code:
static func loginWithEmail(email: String, password: String, response: (token: String?) -> ()) {
let urlString = baseURL + ResourcePath.login.description
let parameters = [
"email": email,
"password": password
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { (_, _, data, _) -> Void in
let json = JSON(data!)
let token = json["token"].string
response(token: token)
}
}
The error is referring to the following line:
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { (_, _, data, _) -> Void in
I am new to swift closures and do not know if I have to nest the values in order for it to be valid.
Help is much appreciated.
The closure takes a single parameter of type Response<AnyObject, NSError> so your code should look more like this.
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { response in
let json = JSON(response.data!)
let token = json["token"].string
response(token: token)
}
Thanks for the help, this was my first time asking here and it was helpful and encouraging. Final code looks like this:
static func loginWithEmail(email: String, password: String, func_response: (token: String?) -> ()) {
let urlString = baseURL + ResourcePath.login.description
let parameters = [
"email": email,
"password": password
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { response in
if response.result.isSuccess {
let json = JSON(response.result.value!)
let token = json["token"].string
func_response(token: token)
}
}
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