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

PHP while loop dynamic rowspan -

android - How to read a column value in parse.com without accessing an object or a pointer -

timer - Java TimerTask cancel -