gpt4 book ai didi

ios - NSURLSessionDownloadTask 快速完成

转载 作者:行者123 更新时间:2023-11-28 05:34:47 26 4
gpt4 key购买 nike

有人可以帮我把这个语句翻译成 Swift 吗?

 NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
// this handler is not executing on the main queue, so we can't do UI directly here
if (!error) {
if ([request.URL isEqual:self.imageURL]) {
// UIImage is an exception to the "can't do UI here"
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:localfile]];
// but calling "self.image =" is definitely not an exception to that!
// so we must dispatch this back to the main queue
dispatch_async(dispatch_get_main_queue(), ^{ self.image = image; });
}
}
}];

completionHandler 应该是这样的吗?

  completionHandler: { (localfile: NSURL!, response: NSURLResponse!, error: NSError!) in ...}

我一输入它就不喜欢冒号的。

最佳答案

var session = NSURLSession.sharedSession()
var request = NSURLRequest(URL: NSURL(string: "http://api.openweathermap.org/data/2.5/weather?q=London,uk"))

var task = session.dataTaskWithRequest(request, completionHandler: { data, response, error in
if !error {
var string = NSString(data: data, encoding: 0)
NSLog("%@", string)
}
})

task.resume()

关于ios - NSURLSessionDownloadTask 快速完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416245/

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