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
Post a Comment