gpt4 book ai didi

ios - URLSession 错误 Swift 3

转载 作者:行者123 更新时间:2023-11-30 12:42:44 29 4
gpt4 key购买 nike

我有以下代码:

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' produces '()', not the expected contextual result type 'URLSession!'.

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

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

How to set URLSession Swift 3

URLSession error

最佳答案

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

关于ios - URLSession 错误 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044764/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com