我收到了以下错误:
使用Xcode-beta 5和Swift。在测试版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!)
}
}
}
}
答案 0 :(得分: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!)
}
}
}
}