Reputation: 831
I'm attempting to use AlamoFire with Swift 1.2 in XCode 6.3. I've fixed most of the problems (i.e. changing as to as!) but I have one that I can't figure out.
The following code - and snippets like it - generates a compile time error with the message "Ambiguous use of 'responseJSON'" at the line 5 ("req.responseJSON(){"). What do I need to change in the AlamoFire library or my code to fix it? Note: I imported the project as described in the documentation and it worked fantastic 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)
}
}
}
}
Upvotes: 6
Views: 3123
Reputation: 11
I ran into the same issue. Updating your Alamofire to the latest version (1.2.2 as the time I wrote the answer) solved the problem for me.
Upvotes: 1
Reputation: 2269
If you wrap the closure in () instead of leaving it trailing it works also. 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)
}
})
}
}
Upvotes: 1
Reputation: 2222
I have also gotten the following to work:
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. by explicitly stating the parameter name of the closure instead of letting it trail after the method paranthesis. It seems that the new compiler has a problem identifying the method otherwise.
Upvotes: 5
Reputation: 831
Separating the trailing closure into its own variable and then calling resonseJSON(closure) fixes the problem, but I'm not sure why. 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)
}
}
Upvotes: 4