请注意,我已经尝试了很多解决方案,例如this,但没有一个解决方案。
这里是screenshot和代码;
func downloadItems() {
let urlE: String = "http://alicelik.me/forios/service.php"
guard let url = URL(string: urlE) else {
print("Error: cannot")
return
}
let configuration = URLSessionConfiguration.default
var session = URLSession(configuration: configuration)
let task = session.dataTask(with: url as URL){
(data, response, error) in
guard error == nil else {
print("error calling GET on /todos/1")
print(error)
}
return()
}
task.resume()
}
这是URLSession func;
@nonobjc func URLSession(session: URLSessionConfiguration, dataTask: URLSessionDataTask, didReceiveData data: NSData) {
self.data.append(data as Data);
}
@nonobjc func URLSession(session: URLSessionConfiguration, task: URLSessionTask, didCompleteWithError error: NSError?) {
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON()
}
}
注意:* func URLSession(session *)中的URLSession和* URLSessionConfiguration给出相同的错误。
答案 0 :(得分:3)
我有同样的问题,通过以下方式解决:
var session = Foundation.URLSession(configuration: configuration)
答案 1 :(得分:1)
更改URLSession func将解决问题。
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()
}
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()
}
}