J A S K I E R
J A S K I E R

Reputation: 2184

Contextual closure type 'Response<AnyObject, NSError> -> Void' expects 1 argument, but 4 were used in closure body

There is Contextual type for closure argument list expects 1 argument, but 4 were specified But my example with additional error checks... What should i do/What should i change? I guess i should put handler at the end ... How i can optimize it ? Is it necessary for "error" check?

class func getUserMarks(_userID: String, handler: (json: JSON) -> ()) {
    // set request params
    Constants.userMarks.params["id"] = _userID
    // send request
    Alamofire.request(.GET, Constants.userMarks.url, parameters: Constants.userMarks.params)
    .responseJSON() {
        (_, _, data, error) in
        // if no errors
        if error == nil {
            // check for coming data
            if let data: AnyObject = data {
                // convert to json and return back
                handler(json: JSON(data))
            } else {
                handler(json: JSON(["serverError": true]))
            }
        } else {
            self.errorCounter++
            if self.errorCounter > 3 {
                self.errorCounter = 0
                handler(json: JSON(["serverError": true]))
            } else {
                self.getUserMarks(_userID: _userID, handler: handler)
            }
        }
    }
}

enter image description here

Upvotes: 0

Views: 2426

Answers (2)

Gowtham Sooryaraj
Gowtham Sooryaraj

Reputation: 3907

just use like this

    Alamofire.request(.POST, Constants.userMarks.url, parameters: Constants.userMarks.params).responseJSON { response in
    let json = JSON(response.data!)
    let token = json["token"].string
    response(token: token)
}

Use single argument

Upvotes: 1

Ali Elsokary
Ali Elsokary

Reputation: 118

You are using an updated version of Alamofire and using this syntax with Alamofire version 3 didn't work for me and gave me the same error. I think you can find you answer here as i did.

Link

Using Alamofire Version 3.3.1 :

Due to Alamofire Version 3 and syntax change old code not work. You can check Alamofire 3.0 Migration Guide.

 override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let swiftyJsonVar = JSON(responseData.result.value!)

        if let resData = swiftyJsonVar["contacts"].arrayObject {
            self.arrRes = resData as! [[String:AnyObject]]
        }
        if self.arrRes.count > 0 {
            self.tblJSON.reloadData()
        }
    }
}

}

Upvotes: 1

Related Questions