gpt4 book ai didi

iOS9 sendSynchronousRequest 弃用

转载 作者:IT王子 更新时间:2023-10-29 05:09:39 25 4
gpt4 key购买 nike

Warning:'sendSynchronousRequest(_:returningResponse:)' was deprecated in iOS 9.0: Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession)

urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)

关于如何摆脱这个警告的任何想法?我刚刚从 Swift 1.2 升级到 Swift 2

更新:Fonix 被标记为我的最佳答案。如果您尝试添加 try 语句,我修改了他的答案如下:

urlData = try NSURLSession.dataTaskWithRequest(<#request: NSURLRequest#>, completionHandler: <#((NSData!, NSURLResponse!, NSError!) -> Void)?##(NSData!, NSURLResponse!, NSError!) -> Void#>)

最佳答案

像下面这样使用NSURLSession

对于 objective-c

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response

}] resume];

对于 Swift,

var request = NSMutableURLRequest(URL: NSURL(string: "YOUR URL"))
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

var params = ["username":"username", "password":"password"] as Dictionary<String, String>

var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")})

task.resume()

关于iOS9 sendSynchronousRequest 弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643207/

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