1

我正在尝试从 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
    }
}

更新: 经过一番挖掘找到解决方案后,请参阅下面的答案以获取更多详细信息和有用的链接

4

1 回答 1

-1

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

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

let URLSession1 = URLSession(configuration: .default)

然后我可以调用'GET'方法,我不得不使用(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: 无法识别的选择器发送到实例

于 2016-12-23T00:06:49.787 回答