Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlamoFire with Swift 1.2: Ambiguous use of 'responseJSON'

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)
            }

        }
    }
}
like image 449
RMH Avatar asked Feb 16 '15 13:02

RMH


2 Answers

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.

like image 95
Joachim Bøggild Avatar answered Sep 30 '22 01:09

Joachim Bøggild


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)


        }
}
like image 43
RMH Avatar answered Sep 30 '22 00:09

RMH