使用Swift 1.2的AlamoFire:模糊地使用'responseJSON'

[英]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.

我試圖在XCode 6.3中使用AlamoFire和Swift 1.2。我已經修復了大部分問題(即改變為!)但我有一個我無法弄清楚的問題。

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

以下代碼 - 以及類似的代碼片段 - 在第5行(“req.responseJSON(){”)生成編譯時錯誤,並顯示消息“不明確地使用'responseJSON'”。我需要在AlamoFire庫或我的代碼中進行哪些更改才能修復它?注意:我按照文檔中的描述導入了項目,它在Swift 1.1和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)
            }

        }
    }
}

4 个解决方案

#1


5  

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.

即通過顯式聲明閉包的參數名稱而不是讓它在方法paranthesis后跟蹤。似乎新編譯器在識別方法時存在問題。

#2


4  

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:

將尾隨閉包分成它自己的變量,然后調用resonseJSON(閉包)修復了這個問題,但我不確定為什么。有人有理由嗎?這是工作代碼:

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)


        }
}

#3


1  

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

    })
  }
}

#4


1  

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.

我遇到了同樣的問題。將Alamofire更新到最新版本(當我寫答案時為1.2.2)為我解決了這個問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/02/16/7298aa0744d9c749542b3f05e7898492.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com