【问题标题】:Alamofire closure arguments error when upgrading to swift 2升级到 swift 2 时 Alamofire 关闭参数错误
【发布时间】: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 的解决方案,但我无法让它在没有错误的情况下正常工作。请帮忙!

【问题讨论】:

标签: xcode swift alamofire


【解决方案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 个

【讨论】:

  • 我不知道如何将变量插入其中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
相关资源
最近更新 更多