ios - fatal error when reloading table data: unwraps to nil: need assistance -


the app developing has table view. seems made mistake when printing table view cell values, because whenever reload table data when app running, fatal error, unexpectedly found nil in unwrapping. happening because there's error in table code. appreciated, thanks. entire code: import uikit

class viewcontroller: uiviewcontroller, uitableviewdelegate { var arr = [int]() var timer = nstimer() var countdowntimer = nstimer() var count = 0 var countdown = int(arc4random_uniform(4000) + 1000) var highscore:int! var buttontapcounter = 0 let dateformatter = nsdateformatter()   @iboutlet weak var beginbutton: uibutton!   @iboutlet weak var table: uitableview! @iboutlet weak var resetdatabutton: uibutton! @iboutlet weak var averagelabel: uilabel! @iboutlet weak var bestlabel: uilabel!      @iboutlet weak var startbutton: uilabel! @iboutlet weak var highscorebutton: uilabel! @iboutlet weak var timerlabel: uilabel! @iboutlet weak var backgroundimage: uiimageview!  func updatetime() {     countdown = countdown - 1     if countdown <= 0 {         startbutton.textcolor = uicolor.blackcolor()         startbutton.text = "tap now!"         countdowntimer.invalidate()         backgroundimage.image = uiimage(named: "red")         count = 0         timer()     } }  func timer() {     timer = nstimer.scheduledtimerwithtimeinterval(0.001, target:self, selector:selector("reflextest"), userinfo:nil, repeats: true)  }  func reflextest() {     count = count + 1     timerlabel.text = "\(count) ms" }    @ibaction func returnpressed(sender: anyobject) {     buttontapcounter = 0 }   @ibaction func scorebuttontouched(sender: anyobject) {     buttontapcounter = 0     table.reloaddata() //the table.reloaddata() here causing problem. //i typing //like //notice scroll //sorry //i typing //like //notice scroll //sorry }  @ibaction func begintapped(sender: anyobject) {     if startbutton.text == "tap when color changes" {         startbutton.text = "you tapped early!"         countdowntimer.invalidate()         countdown = int(arc4random_uniform(4000) + 1000)     } else {         if count == 0 {             countdowntimer = nstimer.scheduledtimerwithtimeinterval(0.001, target: self, selector: selector("updatetime"), userinfo: nil, repeats: true)             startbutton.text = "tap when color changes"             countdown = int(arc4random_uniform(4000) + 1000)             timerlabel.text = "\(count) ms"         } else {             timer.invalidate()             backgroundimage.image = uiimage(named: "green")             startbutton.text = "tap begin"             arr.append(count)             count = 0             highscore = minelement(arr)             highscorebutton.text = "best: \(highscore)"             savedefaults()          }      }  }  func loaddefaults() {      if let savedarray = nsuserdefaults.standarduserdefaults().objectforkey("data") as? [int] {     self.arr = savedarray     } } func savedefaults() {     nsuserdefaults.standarduserdefaults().setobject(self.arr, forkey: "data")  } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     return arr.count }  func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     dateformatter.dateformat = "mm/dd/yy hh:mm"     let dateinformat = dateformatter.stringfromdate(nsdate())      let cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cell")     var tablearray:[int]! = arr     cell.textlabel?.text = string(tablearray[indexpath.row])      return cell }   override func viewdidload() {     super.viewdidload()     loaddefaults()      highscore = minelement(arr)     if buttontapcounter == 0 {         highscorebutton.text = "best: \(highscore)"     }        // additional setup after loading view, typically nib. }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. } 

}

the code needs fixing chunk:

 func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     dateformatter.dateformat = "mm/dd/yy hh:mm"     let dateinformat = dateformatter.stringfromdate(nsdate())      let cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cell")     var tablearray:[int]! = arr     cell.textlabel?.text = string(tablearray[indexpath.row])      return cell } 

once again, thank everyone, , remember: error found nil while unwrapping optional value has table. thanks!

i think problem array(arr). print arr in console. tried code taking static integer array , works fine.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -