Reputation: 1639
When I am terminating the app then my data are successfully saved in document directory but when I start the app again the data is not visible.Here, its code of saving and loading of data.
func saveChecklistItems()
{
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(items, forKey: "Checklists")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}
func loadChecklistItems()
{
let path = dataFilePath()
if NSFileManager.defaultManager().fileExistsAtPath(path)
{
if let data = NSData(contentsOfFile: path)
{
let unarchiver = NSKeyedUnarchiver(forReadingWithData:data)
if let checklistItems = unarchiver.decodeObjectForKey("ChecklistsItems") as? [ChecklistItem]
{
items = checklistItems
}
else
{
unarchiver.finishDecoding()
}
}
}
}
Upvotes: 1
Views: 57
Reputation: 2471
Here you have used the kay as
archiver.encodeObject(items, forKey: "Checklists")
here you have used as ...
unarchiver.decodeObjectForKey("ChecklistsItems") as? [ChecklistItem]
change it to ....
unarchiver.decodeObjectForKey("Checklists") as? [ChecklistItem]
Upvotes: 1