ChecklistViewController:
func saveChecklistItem(){
let data = NSMUTableData()
let archive = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(items, forKey: "ChecklistItems")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}
将该方法以saveChecklistItems()的形式加入到需要存储数据的方法当中。
//初始化时调用loadChecklistItem()
required init(coder aDecoder: NSCoder) {
items = [ChecklistItem]()
super.init(coder:aDecoder)
loadChecklistItem()
}
//从“ChecklistItem”中提取数据
func loadChecklistItem() {
let data = dataFilePath()
if NSFileManager.defaultManager().fileExsistsAtPath(path) {
if let data = NSData(contentOfFile:path {
let unachiver= NSKeyedunachiver(forReadingWithData: data)
items = unachiver.decodeObjectForKey("ChecklistItems") as [ChecklistItem]
unachiver.finishDecoding()