6

This line has always been working fine for me for making Alamofire Requests and getting the JSON response.

Alamofire.request(req).responseJSON() {
        (request, response, data, error) in

    // ....

}

After upgrading to XCode 7 and converting the Project to Swift 2.0, all the lines of code that have my Alamofire request are not showing this error:

'(_, _, _, _) -> Void' is not convertible to 'Response<AnyObject, NSError> -> Void'
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • I'm confused, but the Alamofire doc says that's how it should be called. Even when I took it down to 2 params, still the same error, except it shows (_,_) instead of (_,_,_,_) – Lavvo Sep 22 '15 at 18:15
  • Added an answer below, had to open up an AF project in Xcode7 to see, hope it works! – Fred Faust Sep 22 '15 at 18:27

4 Answers4

14

Found the answer in this link but it is in japanese. It seems this is the correct from now (taken from answer in link):

Alamofire.request(.GET, requestUrl).responseJSON {
   response in
    if response.result.isSuccess {
        let jsonDic = response.result.value as! NSDictionary
        let responseData = jsonDic["responseData"] as! NSDictionary
        self.newsDataArray = responseData["results"] as! NSArray
        self.table.reloadData()
    }            
}
Community
  • 1
  • 1
Braulio Miki
  • 156
  • 1
  • 5
  • I am trying this right now. So far, this is removing my compile errors. I need to do this a few more times in various areas before I can build my app to test, then I'll let you know the results. – Lavvo Sep 22 '15 at 18:44
  • You saved me, this seems to be the correct answer. In short, this is now working for me. Still quite a few tweaks I need to do based on this new form, but I can see my log statements pulling results. – Lavvo Sep 22 '15 at 19:18
  • It seems that if you install Alamofire using Cocoapods, this problem doesn't occur. – Braulio Miki Sep 22 '15 at 21:21
  • Unless you follow the project closely, I'd suggest you roll back to the Alamofire 2.0.2 release until we have a chance to update the documentation and create a migration guide for the Alamofire 3.0.0-beta.1 changes. – cnoon Sep 23 '15 at 15:34
  • 1
    Thanks! The error handling is the key part that this adds that doesn't seem to be covered in the Alamofire 3 migration guide - lots of examples of getting data, but no examples of how error handling is intended to be done. @cnoon Would be nice to have the guide or documentation updated to include some more practical examples, not just printing results. – Geoff Oct 11 '15 at 16:10
2

Old syntax:

Alamofire.request(req).responseJSON() {
  (request, response, data, error) in
   // ....
}

New syntax:

Alamofire.request(req).responseJSON() {
  response in
  if response.result.isSuccess {
    let data = response.result.value
    // ....
  }
}
Petr Lazarev
  • 3,102
  • 1
  • 21
  • 20
0

I pulled up a project with AF and here you go:

Alamofire.request(.POST, someRequest).responseJSON { (request, response, result) -> Void in


    }

Looks like it's just 3 parameters for the closure, request, response & the result object. I'd imagine this is because this should be something that throws in Swift 2.0.

Fred Faust
  • 6,696
  • 4
  • 32
  • 55
0

Using Alamofire-SwiftyJSON the error handling is the same:

.responseSwiftyJSON({ (request, response, json, error) -> Void in
    if let error = error {
        print("Received error \(error)")
        return
    }
    else {
        print("Received json response \(json)")
    }
}

but now error is a ErrorType instead of a NSError.

Using plain Alamofire and iOS JSON, the response and error are unified in a result of type Result<AnyObject>, you have to unwrap the result:

.responseJSON { request, response, result in
    switch result {
    case .Success(let value):
        print("Received response \(value)")
    case .Failure(_, let error):
        print("Received error \(error)")
    }
redent84
  • 18,901
  • 4
  • 62
  • 85