Dennisch Dennisch - 3 months ago 76
iOS Question

"Cannot convert value of type" error in Swift3

I've got the following error:

enter image description here

with Xcode-beta 5 and Swift. In beta 4 it works fine. Anybody who can help me?

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

Answer

Read the error. Look as the type of your error parameter. You've declared it as NSError but the error message is telling you that it should be declared as Error, not NSError.

So you code should be:

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