ios - Swift Optional Type not Unwrapped -
i've been having problem due new updated version of swift. problem line of code keeps producing error saying that:
"value of optional type '()?' not unwrapped; did mean use '!' or '?'?
cell?.hypeimageview?.image = uiimage(data: imagedata)
this entire function:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath, object: pfobject?) -> pftableviewcell? { var cell:hypetableviewcell? = tableview.dequeuereusablecellwithidentifier(cellidentifier) as? hypetableviewcell if(cell == nil) { cell = nsbundle.mainbundle().loadnibnamed("hypetableviewcell", owner: self, options: nil)[0] as? hypetableviewcell } if let pfobject = object { cell?.hypenamelabel?.text = pfobject["name"] as? string var votes:int? = pfobject["votes"] as? int if votes == nil { votes = 0 } cell?.hypevoteslabel?.text = "\(votes!) votes" let credit:string? = pfobject["cc_by"] as? string //if prob change var if credit != nil { cell?.hypecreditlabel?.text = "\(credit!) / cc 2.0" } cell?.hypeimageview?.image = nil if var urlstring:string? = pfobject["url"] as? string { var url:nsurl? = nsurl(string: urlstring!) if var url:nsurl? = nsurl(string: urlstring!) { var error:nserror? var request:nsurlrequest = nsurlrequest(url: url!, cachepolicy: nsurlrequestcachepolicy.returncachedataelseload, timeoutinterval: 5.0) nsoperationqueue.mainqueue().cancelalloperations() nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue(), completionhandler: { (response:nsurlresponse!, imagedata:nsdata!, error:nserror!) -> void in cell?.hypeimageview?.image = uiimage(data: imagedata) }) } } } return cell }
how can fix issue?
assuming you're using swift 2.0 change signature completionhandler
expect optionals rather implicitly unwrapped optionals, problem go away.
nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()) { (response:nsurlresponse?, data:nsdata?, error:nserror?) -> void in guard let imagedata = data else { assertionfailure("no data found") return } cell?.hypeimageview?.image = uiimage(data: imagedata) }
Comments
Post a Comment