ios - App not deleting a row from .plist file -


in project working on have note-esque function acting exercise/training log. training log made of 5 files: note.swift, notestableviewcontroller.swift, notedetailviewcontroller.swift, notedetailtableviewcell.swift, , notestore.swift. class table notestableviewcontroller, uiviewcontroller uitableviewdelegate, , uitableviewdatasource. note taking feature works decently, populating tableview, fails delete note .plist file , continues retrieve when app reopened. not know if failure save/load, or if going wrong somewhere else. appreciate @ all. files follows:

note.swift

import foundation  class note : nsobject, nscoding { var title = "" var text = "" var date = nsdate() // defaults current date / time  // computed property return date string var shortdate : nsstring {     let dateformatter = nsdateformatter()         dateformatter.dateformat = "mm/dd/yy"         return dateformatter.stringfromdate(self.date) }  override init() {     super.init() }  // 1: encode ourselves... func encodewithcoder(acoder: nscoder) {     acoder.encodeobject(title, forkey: "title")     acoder.encodeobject(text, forkey: "text")     acoder.encodeobject(date, forkey: "date") }  // 2: decode ourselves on init required init(coder adecoder: nscoder) {     self.title = adecoder.decodeobjectforkey("title") as! string     self.text  = adecoder.decodeobjectforkey("text") as! string     self.date   = adecoder.decodeobjectforkey("date") as! nsdate }  } 

notestableviewcontroller.swift

import uikit  class notestableviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {  @iboutlet weak var tableview: uitableview!  @iboutlet weak var openbutton: uibarbuttonitem!  override func viewdidload() {     super.viewdidload()      // leverage built in tableviewcontroller edit button     self.navigationitem.leftbarbuttonitem = self.editbuttonitem()      openbutton.target = self.revealviewcontroller()     openbutton.action = selector("revealtoggle:")      self.view.addgesturerecognizer(self.revealviewcontroller().pangesturerecognizer())      self.tableview.reloaddata()  }  override func viewwillappear(animated: bool) {     super.viewwillappear(animated)     // ensure not in edit mode     editing = false }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // here pass note tapped on between view controllers     if segue.identifier == "notedetailpush" {         // controller going         var notedetail = segue.destinationviewcontroller as! notedetailviewcontroller         // lookup data want pass         var thecell = sender as! notedetailtableviewcell         // pass data forward         notedetail.thenote = thecell.thenote     } }   @ibaction func savefromnotedetail(segue:uistoryboardsegue) {     // come here exit segue when hit save on detail screen      // controller coming     var notedetail = segue.sourceviewcontroller as! notedetailviewcontroller      // if there row selected....     if let indexpath = tableview.indexpathforselectedrow() {         // update note in our store         notestore.sharednotestore.updatenote(thenote: notedetail.thenote)          // user in edit mode         tableview.reloadrowsatindexpaths([indexpath], withrowanimation: uitableviewrowanimation.automatic)     } else {         // otherwise, add new record         notestore.sharednotestore.createnote(thenote: notedetail.thenote)          // index insert row @         var indexpath = nsindexpath(forrow: notestore.sharednotestore.count()-1, insection: 0)          // update tableview         tableview.insertrowsatindexpaths([indexpath], withrowanimation: uitableviewrowanimation.automatic)     } }  // mark: - table view data source    func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     // return note count     return notestore.sharednotestore.count() }    func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     // fetch reusable cell     let cell = tableview.dequeuereusablecellwithidentifier("notedetailtableviewcell", forindexpath: indexpath) as! notedetailtableviewcell      // fetch note     var rownumber = indexpath.row     var thenote = notestore.sharednotestore.getnote(rownumber)      // configure cell     cell.setupcell(thenote)      return cell }   // override support editing table view.  func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {      if editingstyle == .delete {         // delete row data source         notestore.sharednotestore.deletenote(indexpath.row)         // delete note tableview         tableview.deleterowsatindexpaths([indexpath],        withrowanimation: .fade)     } }   } 

notedetailviewcontroller

import uikit  class notedetailviewcontroller: uiviewcontroller {  var thenote = note()  @iboutlet weak var notetitlelabel: uitextfield!  @iboutlet weak var notetextview: uitextview!  override func viewdidload() {     super.viewdidload()      // view starts here. either have note edit     // or have blank note in thenote property can use      // update screen contents of thenote     self.notetitlelabel.text = thenote.title     self.notetextview.text = thenote.text      // set cursor in note text area     self.notetextview.becomefirstresponder() }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // whenever leave screen, update our note model     thenote.title = self.notetitlelabel.text     thenote.text = self.notetextview.text }  @ibaction func cancelnote(sender: anyobject) {      self.dismissviewcontrolleranimated(true, completion: nil)  } } 

notedetailtableviewcell

import uikit  class notedetailtableviewcell : uitableviewcell {  // note being shown weak var thenote : note!  // interface builder outlets @iboutlet weak var notetitlelabel : uilabel! @iboutlet weak var notedatelabel : uilabel! @iboutlet weak var notetextlabel : uilabel!  // insert note contents cell func setupcell(thenote:note) {     // save weak reference note     self.thenote = thenote      // update cell     notetitlelabel.text = thenote.title     notetextlabel.text = thenote.text     notedatelabel.text = thenote.shortdate string }  } 

and finally, notestore

import foundation  class notestore { // mark: singleton pattern (hacked since don't have class var's yet) class var sharednotestore : notestore { struct static {     static let instance : notestore = notestore()     }     return static.instance }  // private init force usage of singleton private init() {     load() }  // array hold our notes private var notes : [note]!  // crud - create, read, update, delete  // create  func createnote(thenote:note = note()) -> note {     notes.append(thenote)     return thenote }  // read  func getnote(index:int) -> note {     return notes[index] }  // update func updatenote(#thenote:note) {     // notes passed reference, no update code needed }  // delete func deletenote(index:int) {     notes.removeatindex(index) }  func deletenote(withnote:note) {      (i, note) in enumerate(notes) {         if note === withnote {             notes.removeatindex(i)             return         }     }  }  // count func count() -> int {     return notes.count }   // mark: persistence  // 1: find file & directory want save to... func archivefilepath() -> string {     let paths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)     let documentsdirectory = paths.first as! nsstring     let path = documentsdirectory.stringbyappendingpathcomponent("notestore.plist")      return path }  // 2: save disk..... func save() {     nskeyedarchiver.archiverootobject(notes, tofile: archivefilepath()) }   // 3: reload disk.... func load() {     let filepath = archivefilepath()     let filemanager = nsfilemanager.defaultmanager()      if filemanager.fileexistsatpath(filepath) {         notes = nskeyedunarchiver.unarchiveobjectwithfile(filepath) as! [note]     } else {         notes = [note]()     } }  } 

it look's you're not calling save method after changing creating,deleting or updating notes

you add example :

func deletenote(index:int) { notes.removeatindex(index) save()    } 

or call save methods on vievwilldisappear if don't want write new plist after every change


Comments