ios - Swift - tableview.reloadData() crashes the app -


i fetching data plist uitableview. trying delete data selected however, when try reload data show remaining cells app crashes. think problem when use tableview.reloaddata() not sure how fix problem. if don't use reloaddata cell deleted when reopen view controller.

any advice?

func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {         if editingstyle == uitableviewcelleditingstyle.delete {              let row = indexpath.row              let plistpath = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true) nsarray             let documentsdirectory = plistpath[0] as! string             let path = documentsdirectory.stringbyappendingpathcomponent("notes.plist")             let filemanager = nsfilemanager.defaultmanager()              if (!filemanager.fileexistsatpath(path)) {                  if let bundlepath = nsbundle.mainbundle().pathforresource("notes", oftype: "plist") {                      let resultdictionary = nsmutabledictionary(contentsoffile: bundlepath)                     println("bundle notes.plist file --> \(resultdictionary?.description)")                     filemanager.copyitematpath(bundlepath, topath: path, error: nil)                     println("copy")                  } else {                     println("notes.plist not found")                 }               } else {                 println("note.plist exists")                  //filemanager.removeitematpath(path, error: nil)             }              let resultdictionary = nsmutabledictionary(contentsoffile: path)             //resultdictionary?.removeallobjects()             resultdictionary?.removeobjectforkey(allkeys[row])             resultdictionary!.writetofile(path, atomically: false)              println("loaded notes.plist file --> \(resultdictionary?.description)")               tableview.reloaddata()          }     }  

enter image description here

about calling reloaddata(), documentation says: "it should not called in methods insert or delete rows, within animation block implemented calls beginupdates , endupdates." better reload section made changes , call begin , end if animation involved

tableview.beginupdates() tableview.reloadrowsatindexpaths(path, withrowanimation: uitableviewrowanimation.automatic) tableview.endupdates() 

and preferable use own instance of nsfilemanager since default 1 works in main thread. , you're unsafely unwrapping resultdictionary when writing file, cause crash ps,

let path = documentdirectory.stringbyappendingpathcomponent("notes.plist") 

is replaced stringbyappendingstring n swift 2, fyi


Comments