2016-12-05 80 views
1

我的UIImageView上的擴展名給我一個警告。我想要一個免費的警告項目,但我不確定如何正確解決此問題。我收到的警告是:sendAsynchronousRequest在iOS 9中不推薦使用,我該如何解決我的功能

sendAsynchronousRequest在iOS的9.0 depricated:使用[NSURLSession dataTAskWithRequest ....

是造成該警告的延伸是這樣的:

extension UIImageView { 
    public func imageFromUrl(urlString: String) { 
     if let url = NSURL(string: urlString) { 
      let request = NSURLRequest(URL: url) 
      NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { 
       (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in 
       if let imageData = data as NSData? { 
        self.image = UIImage(data: imageData) 
       } 
      } 
     } 
    } 
} 
+0

使用 「URLSession」 –

回答

2

嘗試此

雨燕3.0

URLSession.shared.dataTask(with: request) {data, response, error in 
    if (error == nil) { 
     //Do needful things // Your code . . . 
    } else { 
     print("Error : \(error?.localizedDescription)"); 
    } 
}.resume(); 
1

你清楚地警告說,sendAsynchronousRequest was depricated in iOS 9.0: Use [NSURLSession dataTAskWithRequest這意味着你應該使用NSURLSession代替NSURLConnection

你應該這樣做,

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

task.resume() 
相關問題