BroSimple BroSimple - 4 days ago 5
JSON Question

Error in Swift SDWebImage

I'm using

SDWebImage
in my Swift app to download images to my tableview but I'm getting a simple error that I don't know how to fix. I finished converting my project from Objective C to Swift.

Code I'm using

let imgURL = (testObject.value(forKey: "testURL"))
self.myImageView.sd_setImage((with: imgURL as! URL!), placeholderImage: UIImage(imageNamed: "no-image.png"))
self.myImageView.contentMode = .scaleAspectFit


Error:


Argument labels '(imageNamed:)' do not math any available overloads

Answer

You can try correct naming for UIImage initializer, and improved cast to URL:

let imgURL = testObject.value(forKey: "testURL") as! URL
self.myImageView.sd_setImage(with: imgURL, placeholderImage: UIImage(named: "no-image.png"))

But better, you can use an image literal for "no-image":

let imgURL = testObject.value(forKey: "testURL") as! URL
self.myImageView.sd_setImage(with: imgURL, placeholderImage: #imageLiteral(resourceName: "no-image.png"))

And best would be to avoid the forced unwrap.

let imgURL = testObject.value(forKey: "testURL") as? URL
self.myImageView.sd_setImage(with: imgURL, placeholderImage: #imageLiteral(resourceName: "no-image.png"))

[update: after author claims that value is a String]

If value of "testURL" is a String, then you can't unwrap it as an URL. This code may help:

let imgString = testObject.value(forKey: "testURL") as? String ?? ""
self.myImageView.sd_setImage(with: URL(string: imgString), placeholderImage: #imageLiteral(resourceName: "no-image.png"))
Comments