升级到 swift 2 时 Alamofire 关闭参数错误

Posted

技术标签:

【中文标题】升级到 swift 2 时 Alamofire 关闭参数错误【英文标题】:Alamofire closure arguments error when upgrading to swift 2 【发布时间】:2015-11-26 00:02:05 【问题描述】:

我在使用 Alamofire 编译我在 Swift 1 中创建的应用程序时遇到问题。问题出现在以下代码中:

func fetchApiData() 

    print("called")

    // I believe this is the problem code below.


    let _requestURL1 = Alamofire.request(.GET,dataSourceURL!)
    _requestURL1.responseJSON  (_requestUrl, _requestResponse, _objJSON1, error) -> Void in
        if(_objJSON1 != nil)
        
            let jsonResult1 = _objJSON1 as NSDictionary;
            //let jsonResult2: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data,
            //    options:NSJSONReadingOptions.MutableContainers, error: self.error) as NSDictionary
            self.checkIP(jsonResult1)
            self.checkGeo(jsonResult1)
            //println(jsonResult1);
        
        else
            return
        

给出的错误说:

Contextual type for closure argument list expects 1 argument, but 4 were specified

我已经尝试了here 的解决方案,但我无法让它在没有错误的情况下正常工作。请帮忙!

【问题讨论】:

Alamofire POST request with Swift 2的可能重复 【参考方案1】:

代码应该是这样的

let _requestURL1 = Alamofire.request(.GET,dataSourceURL!)

_requestURL1.responseJSON  response in
let json = JSON(response.data!)
let token = json["token"].string
response(token: token)

正如在另一篇文章中描述的那样,在 Swift 2 中,.responseJSON 从 4 个参数变为只有 1 个

【讨论】:

我不知道如何将变量插入其中

以上是关于升级到 swift 2 时 Alamofire 关闭参数错误的主要内容,如果未能解决你的问题,请参考以下文章