注意不会删除

我有我的应用程序的笔记部分,我可以创build注释和编辑笔记,但是当涉及到删除笔记我有一个问题。 我可以从表格视图本身和所有注释中删除它们,但是当我重新加载应用程序时,它们回来,就好像它们粘在字典中一样。 你能否请求我永久删除它们? 我有三个控制器,只有两个是相关的。 这里是相关的:

MasterViewController.swift

import UIKit class MasterViewController: UITableViewController { @IBOutlet weak var menuButton: UIBarButtonItem! override func awakeFromNib() { super.awakeFromNib() } override func viewDidLoad() { super.viewDidLoad() Note.loadnotes() noteTable = self.tableView // Side Menu if self.revealViewController() != nil { menuButton.target = self.revealViewController() menuButton.action = "revealToggle:" self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) } // Do any additional setup after loading the view, typically from a nib. func insertNewObject(sender: AnyObject) { allNotes.insert(Note(), atIndex: 0) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) self.performSegueWithIdentifier("showDetail", sender: self) } // MARK: - Segues override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { if let indexPath = self.tableView.indexPathForSelectedRow() { let object: Note = allNotes[indexPath.row] as Note currentNoteIndex = indexPath.row } else { currentNoteIndex = 0 } } } // MARK: - Table View override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return allNotes.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell let object: Note = allNotes[indexPath.row] as Note; cell.textLabel!.text = object.note return cell } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { allNotes.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } } 

note.swift

 import UIKit var allNotes:[Note] = [] var currentNoteIndex:NSInteger = -1 var noteTable:UITableView? let KAllNotes:String = "notes" class Note: NSObject { var date:String var note:String override init() { date = NSDate().description note = "" } func dictionary() -> NSDictionary { return ["note":note, "date":date] } class func saveNotes() { var aDictionaries:[NSDictionary] = [] for (var i:NSInteger = 0; i < allNotes.count; i++) { aDictionaries.append(allNotes[i].dictionary()) } NSUserDefaults.standardUserDefaults().setObject(aDictionaries, forKey: KAllNotes) // aDictionaries.writeToFile(filePath(), atomically: true) } class func loadnotes() { var defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() var savedData:[NSDictionary]? = defaults.objectForKey(KAllNotes) as? [NSDictionary] // var savedData:NSArray? = NSArray(contentsOfFile: filePath()) if let data:[NSDictionary] = savedData { for (var i:NSInteger = 0; i < data.count; i++) { var n:Note = Note() n.setValuesForKeysWithDictionary(data[i] as [NSObject : AnyObject]) allNotes.append(n) } } } class func filePath() -> String { var d:[String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] if let directories:[String] = d { var docsDirectory:String = directories[0] var path:String = docsDirectory.stringByAppendingPathComponent("\(KAllNotes).notes") return path; } return "" } } 

在此先感谢山姆

在主视图控制器中使用这个改变的function:

 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { allNotes.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) Note.saveNotes() } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } 

这将迫使字典重新保存。 在notes.swift中使用您的保存笔记function