0

我有以下代码:

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()

}

这是来自一个较旧的在线项目,Swift 给了我这个错误代码:

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

我发现其他人在 Stock Overflow 上询问这个问题,但所有答案都是针对旧版本的 Swift,并导致其他错误。

以下是我发现的其他问题:

如何设置 URLSession Swift 3

URLSession 错误

4

1 回答 1

0
var request = URLRequest(url: URL(string: urlPath!))
request.httpMethod = "GET"
task1 = URLSession.shared.dataTask(with: request) { data, response, error 

in guard let data = data, error == nil else {                                                 

// check for fundamental networking error

   print("error=\(error)")

 return

}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {


   // check for http errors

   print("statusCode should be 200, but is \(httpStatus.statusCode)")

   print("response = \(response)")

}

let json=JSON(data: data);

self.json=json;

self.parse(json: json)

}

task1.resume()
于 2017-05-17T14:31:24.277 回答