【问题标题】:Problems with migration to Swift3, compilation error with URLSession.GET迁移到 Swift3 的问题,URLSession.GET 的编译错误
【发布时间】:2016-12-21 19:31:53
【问题描述】:

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

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

【问题讨论】:

标签: swift3 migration nsurlsession promisekit


【解决方案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: unrecognized selector sent to instance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2012-09-30
    相关资源
    最近更新 更多