Alamofire + SwiftyJSON Compile Error after converting to Xcode 7
Asked Answered
H

4

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'
Hallucinosis answered 22/9, 2015 at 17:44 Comment(2)
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 (,,,)Hallucinosis
Added an answer below, had to open up an AF project in Xcode7 to see, hope it works!Glans
C
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()
    }            
}
Civilized answered 22/9, 2015 at 18:20 Comment(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.Hallucinosis
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.Hallucinosis
It seems that if you install Alamofire using Cocoapods, this problem doesn't occur.Civilized
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.Haphtarah
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. @Haphtarah Would be nice to have the guide or documentation updated to include some more practical examples, not just printing results.Veroniqueverras
D
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
    // ....
  }
}
Dubonnet answered 23/9, 2015 at 4:35 Comment(0)
G
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.

Glans answered 22/9, 2015 at 18:26 Comment(0)
E
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)")
    }
Enviable answered 22/9, 2015 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.