迁移到Swift3 - >对成员' dataTask的不明确引用(with:CompletionHandler:)'

时间:2016-11-02 12:41:21

标签: swift3

我已将我的项目(最初在Swift 2.2中开发)迁移到Swift 3,但我遇到以下代码的错误:

    let url:URL = URL(string: url_to_request)!
    //let session = NSURLSession.sharedSession()
    let configuration = URLSessionConfiguration.default
    let session = Foundation.URLSession(configuration: configuration,
                               delegate: self,
                               delegateQueue:OperationQueue.main)

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
    request.timeoutInterval = 10

    let task = session.dataTask(with: request, completionHandler: {
        (
        data, response, error) in

        if(response == nil){
            print("erreur de connexion....")
            result = false
            callback(result)
        }

我有以下错误

  

对成员' dataTask的不明确引用(with:CompletionHandler:)'

我已经看到Swift3我们可能会使用NSURLSession.sharedSession(),但由于我使用的是自定义会话,因此我无法正常工作......

我知道如何解决它?

1 个答案:

答案 0 :(得分:1)

编译器需要原生URLRequest而不是NSMutableURLRequest

var request = URLRequest(url: url)
// request.httpMethod = "GET" <- GET is the default
request.cachePolicy = .reloadIgnoringLocalCacheData // <- renamed enum case
request.timeoutInterval = 10