AlamoFire with Swift 1.2: Ambiguous use of "responseJSON"

I am trying to use AlamoFire with Swift 1.2 in Xcode 6.3. I fixed most of the problems (i.e. changed them like!), But I have one that I cannot understand.

The following code - and similar snippets - generates a compile-time error with the message "Ambiguous use of" responseJSON "on line 5 (" req.responseJSON () {"). What do I need to change in the AlamoFire library or my code to fix this? Note I imported the project as described in the documentation, and it worked great in Swift 1.1 and Xcode 6.1.1.

func theaters(delegate:GlobalNetworkingDelegate){ if let url = self.mainNetworkingUrl{ var urlToUse = url + "theaters" var req:Request = Alamofire.request(.GET, urlToUse, parameters: [:], encoding: .URL) req.responseJSON(){ (req, response, jsonOut, error) in if(response.statusCode == 200 && error == nil){ var ajson = JSON(jsonOut!) delegate.globalTheatersOutomce!(true, json: jsonOut, error: error) } } } } 
+6
source share
4 answers

I also got the following:

  Alamofire.manager.request(.PUT, pathWithId(user.key), parameters: user.toDict(), encoding: .JSON) .responseString( completionHandler: { (request: NSURLRequest, response: NSHTTPURLResponse?, responseBody: String?, error: NSError?) -> Void in if responseBody == "OK" { completion(user, nil) } else { completion(nil, error) } }) 

i.e. explicitly specifying the name of the closure parameter rather than skipping it after the paranthesis method. It seems that the new compiler has a problem identifying the method differently.

+5
source

Separating the trailing closure by its own variable and then calling resonseJSON (closure) fixes the problem, but I'm not sure why. Does anyone have a reason? Here is the working code:

 func theaters(delegate:GlobalNetworkingDelegate){ if let url = self.mainNetworkingUrl{ var urlToUse = url + "theaters" var req:Request = Alamofire.request(.GET, urlToUse, parameters: [:], encoding: .URL) var aClosure = {(req:NSURLRequest, response:NSHTTPURLResponse?, jsonOut:AnyObject?, error:NSError?) -> Void in if(response!.statusCode == 200 && error == nil){ var ajson = JSON(jsonOut!) delegate.globalTheatersOutomce!(true, json: jsonOut, error: error) } } req.responseJSON(aClosure) } } 
+4
source

If you close a closure in () rather than leave it, then it also works. It works for the same reason as the other answers here, just another way to write it.

 func theaters(delegate:GlobalNetworkingDelegate){ if let url = self.mainNetworkingUrl { var urlToUse = url + "theaters" var req:Request = Alamofire.request(.GET, urlToUse, parameters: [:], encoding: .URL) req.responseJSON({ (req, response, jsonOut, error) in if(response.statusCode == 200 && error == nil){ var ajson = JSON(jsonOut!) delegate.globalTheatersOutomce!(true, json: jsonOut, error: error) } }) } } 
+1
source

I ran into the same problem. Updating your Alamofire to the latest version (1.2.2 as the time I wrote the answer) solved the problem for me.

+1
source

Source: https://habr.com/ru/post/982558/


All Articles