首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLSessionDownloadTask在斯威夫特

NSURLSessionDownloadTask在斯威夫特
EN

Stack Overflow用户
提问于 2014-06-25 18:46:19
回答 1查看 2.2K关注 0票数 1

有人能帮我把这句话翻译成Swift吗?

代码语言:javascript
复制
 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应该是这样的吗?

代码语言:javascript
复制
  completionHandler: { (localfile: NSURL!, response: NSURLResponse!, error: NSError!) in ...}

我一进去,它就不喜欢冒号了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-25 19:32:43

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24416245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档