Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contextual type for closure argument list expects 1 argument, but 4 were specified

Tags:

swift

swift2

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.

like image 587
Rodrigo Ledesma Avatar asked Oct 28 '15 06:10

Rodrigo Ledesma


2 Answers

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)
}
like image 54
Chris Gulley Avatar answered Oct 05 '22 18:10

Chris Gulley


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)
        }
    }
like image 37
Rodrigo Ledesma Avatar answered Oct 05 '22 20:10

Rodrigo Ledesma