"无法转换类型" Swift3中的错误

时间:2016-08-10 15:32:51

标签: ios swift swift3

我收到了以下错误:

enter image description here

使用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!)
            }
        }
    }
}

1 个答案:

答案 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!)
            }
        }
    }
}