1

我正在使用一个实际上是为 swift 2 编写的函数。我已经对 swift 3 进行了审核。但我不断收到错误消息:

URLSession' 产生 '()',而不是预期的上下文结果类型 'URLSession!

我的代码如下所示:

func downloadItems() {

        let url: NSURL = NSURL(string: urlPath)!
        var session: URLSession!
        let configuration = URLSessionConfiguration.default


        session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)


        let task = session.dataTask(with: url as URL)

        task.resume()
    }

有人知道我在做什么错吗?

4

2 回答 2

0

将您的 URLSession 函数替换为:

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    self.data.append(data as Data)  
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
        print("Failed to download data")
    }else {
        print("Data downloaded")
        self.parseJSON()
    }
}
于 2016-12-02T22:19:45.077 回答
0

代替 :

 session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

做这个:

   session = {
        let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
        return session
    }()
于 2016-11-28T22:09:08.993 回答