ios - How to use pass nil to URL in SDWebImage with Swift -


i using sdwebimage function in swift controller.

sd_setimagewithurl:(nsurl *)url placeholderimage:(uiimage *)placeholder completed:(sdwebimagecompletionblock)completedblock; 

in swift displaying as

let url  = nsurl(string:ventmodel.backgroundblurredurl) let img = uiimage(named: "") cell.courseimgview.sd_setimagewithurl(url, placeholderimage: img) { (uiimage img, nserror err, sdimagecachetype cachetype, nsurl imgurl) -> void in      } 

now lets in model i.e. ventmodel cannot imageurl i.e. nil. app give fatal error , crashes.

you should declare backgroundblurredurl optional like

var backgroundblurredurl: string? 

and unwrap url:

if let url  = nsurl(string:ventmodel.backgroundblurredurl) {    let img = uiimage(named: "")    cell.courseimgview.sd_setimagewithurl(url, placeholderimage: img) {        (uiimage img, nserror err, sdimagecachetype cachetype, nsurl imgurl) -> void in             // awesome things        } } 

backgroundblurredurl not set in case server doesn't send data it.

update:

if it's in objective c, can directly check backgroundblurredurl != nil


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -