Alamofire + SwiftyJSON error after conversion to Xcode 7

This line always worked fine for me to create Alamofire requests and get a JSON response.

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

After upgrading to Xcode 7 and converting Project to Swift 2.0, all lines of code that have my Alamofire request do not show this error:

 '(_, _, _, _) -> Void' is not convertible to 'Response<AnyObject, NSError> -> Void' 
+6
source share
4 answers

Found the answer in this link , but it is in Japanese. This seems to be correct now (taken from the answer by reference):

 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() } } 
+14
source

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 // .... } } 
+2
source

I started the project with AF, and here you go:

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

It seems that these are just 3 parameters for closing, request, response and result object. I think this is because it must be something that throws in Swift 2.0.

0
source

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 ErrorType instead of NSError .

Using simple Alamofire and iOS JSON, the response and error are unified as a result of type Result<AnyObject> , you need to expand 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)") } 
0
source

All Articles