KML
KML

Reputation: 2322

Object is nil but code interpret not as nil

I want to check if an object I get from the server is nil or not, if nil I want to use a placeholder image, if not nil I want to use the image from the url. Debugger says imageUrl is nil, but my code evaluates it as not nil.
the code always crash at

providerImg.sd_setImageWithURL(NSURL(string: GPProfiles[indexPath.row]["image"] as! String))

enter image description here

I have also tried this

let imageUrl = GPProfiles[indexPath.row]["image"]
if imageUrl != nil {
    providerImg.sd_setImageWithURL(NSURL(string: GPProfiles[indexPath.row]["image"] as! String))
} else{
    providerImg.image = UIImage(named: "placeholderImage.png")
}

How can I check if an object is nil ??? Thanks

UPDATE TO ANSVWER BY @andre-slotta enter image description here

Upvotes: 1

Views: 44

Answers (1)

André Slotta
André Slotta

Reputation: 14030

try this:

if let imageUrl = GPProfiles[indexPath.row]["image"] as? String {
    ...
}

since your != nil check does not seem to work here you get some more info about nil, NULL and NSNull: nshipster.com/nil

Upvotes: 2

Related Questions