ios - Changed Cell now - fatal error: unexpectedly found nil while unwrapping an Optional value -


so working cells in uitablview using swift. have been trying change cell title title , subtitle:

my previous code was:

var objects = ckrecord  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let reuseidentifier = "cell"     var cell:uitableviewcell? = tableview.dequeuereusablecellwithidentifier(reuseidentifier) uitableviewcell?     if (cell != nil) {         cell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: reuseidentifier)     }      let object = objects[indexpath.row]     cell!.textlabel!.text = object.objectforkey("notes") as? string     cell!.detailtextlabel?.text = object.creationdate.objectforkey("notes") as? string     return cell! } 

which changed to:

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let reuseidentifier = "cell"         var cell:uitableviewcell? = tableview.dequeuereusablecellwithidentifier(reuseidentifier) uitableviewcell?         if (cell != nil) {             cell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: reuseidentifier)         }          let object = objects[indexpath.row]         cell!.textlabel!.text = object.objectforkey("notes") as? string         var time = object.creationdate          let formatter = nsdateformatter()         formatter.datestyle = nsdateformatterstyle.longstyle         formatter.timestyle = .mediumstyle          let formattedtime = formatter.stringfromdate(time)          cell!.detailtextlabel?.text = formattedtime          return cell! 

on first run code works fine , title (cell content) , subtitle (date) appear perfectly!

but..

when try add new note(which adds new cell) within app following error:

fatal error: unexpectedly found nil while unwrapping optional value

i'm guessing having use "!" in new code, can't figure out how fix issue.

this function adding:

let record = ckrecord(recordtype: "cloudnote") record.setobject("newnote", forkey: "notes") myclipmanager.savemethod(database!, myrecord:record) /// need create if statement make sure record save happened, complete below code self.objects.append(record) self.objects = self.objects.sort({ $0.creationdate?.compare(($1.creationdate)!) == nscomparisonresult.ordereddescending })      self.refresh() } 

previous question reference:

select creationdate cloudkit record


Comments