迁移到Swift3的问题,URLSession.GET的编译错误

时间:2016-12-21 19:31:54

标签: swift3 migration nsurlsession promisekit

我正在尝试从Swift2迁移到Swift3,而我遇到了一个URLSession.GET问题

我浏览了XCode提供的迁移工具,并将我的“NSURLSession”调用更改为“URLSession”但我仍然收到此错误消息:

"Use of Instance member 'GET' on type 'URLSession'; did you mean to use a value of type 'URLSession' instead?"

这是我的代码:

open static func getData() -> Promise<[Station]> {
   return URLSession.GET("http://api.xxx.gov/api/").asDataAndResponse().then { (data: NSData, _) -> [Station] in

     var stations = [Station]()
     // rest of the code to retrieve the data
     ...
     ...

     return stations
    }
}

更新 在找到一个解决方案之后,请参阅下面的答案以获取更多详细信息和有用的链接

1 个答案:

答案 0 :(得分:-1)

经过多次挖掘后,我找到了解决方案:

首先,我必须使用配置初始化URLSession:

let URLSession1 = URLSession(configuration: .default)

然后我可以打电话给&#39; GET&#39;方法,我不得不使用(data,_ )而不是(data:NSData,_ )

open static func getData() -> Promise<[Station]> {
   return URLSession1.GET("http://api.xxx.com/api/").asDataAndResponse().then { (data, _) -> [Station] in

    var stations = [Station]()
    // rest of the code to retrieve the data
    ...
    ...
    return stations
  }
}

帮助我找到解决方案的一些有用的StackOverflow链接:

NSURLSession dataTaskForRequest:completion: unrecognized selector sent to instance