2016-08-10 50 views
2

我已經得到了以下錯誤:在Xcode-β5和斯威夫特「無法將類型的價值」錯誤Swift3

enter image description here

。在測試版4中,它工作正常。任何人都可以幫助我?

extension UIImageView { 
    public func imageFromUrl(_ urlString: String) { 
     if let url = URL(string: urlString) { 
      let request = URLRequest(url: url) 
      NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) { 
       (response: URLResponse?, data: Data?, error: NSError?) -> Void in 
       self.image = UIImage(data: data!) 
      } 
     } 
    } 
} 
+2

從NSError的變化誤差*記錄*在beta 5版本說明。 –

回答

4

閱讀錯誤。看看你的error參數的類型。您已將其聲明爲NSError,但錯誤消息告訴您應將其聲明爲Error,而不是NSError

所以,你的代碼應該是:

extension UIImageView { 
    public func imageFromUrl(_ urlString: String) { 
     if let url = URL(string: urlString) { 
      let request = URLRequest(url: url) 
      NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) { 
       (response: URLResponse?, data: Data?, error: Error?) -> Void in 
       self.image = UIImage(data: data!) 
      } 
     } 
    } 
} 
相關問題