swift - Why i am not able to pass optional value ( ? ) for getting image url? -


self.imgview .sd_setimagewithurl(nsurl(string: dictdata["image"] as? string)) 

hello using swift , want image url dictdata when write line

dictdata["image"] as? string

it's giving error value of optional type 'string?' not unwrapped; did mean use '!' or '?'? , when click on error improve line of code this

dictdata["image"] as! string

why happened? want know reason behind that.

that means dictdata["image"] as? string optional. , nsurl(string) takes non optional parameter. in order have unwrap optional. dictdata["image"] as! string force unwrapping means, if dictdata["image"] nil or if fails cast string app crash. encourage use following code

if let image = dictdata["image"] as? string {         self.imgview .sd_setimagewithurl(nsurl(string: image)) } else {         print("failed cast string") } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -