2014-06-25 1 views
1

Quelqu'un peut-il aider à traduire cette déclaration en Swift pour moi?NSURLSessionDownloadTask dans 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; }); 
        } 
       } 
      }]; 

est-ce que le completionHandler devrait être quelque chose comme ça?

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

Dès que je suis entré cela n'a pas aimé les deux points.

Répondre

2
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() 
+0

Parfait! une réponse facile à une question stupide. Pensez qu'il est temps de l'appeler un jour pour moi – Sawyer05