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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -