NSKeyedArchiver and NSKeyedUnarchiver
You now have a place to save data on the filesystem and a model object that can be saved to the filesystem. The final two questions are: how do you kick off the saving and loading processes, and when do you do it? To save instances of Item, you will use the class NSKeyedArchiver when the application “exits.”
In ItemStore.swift, implement a new method that calls archiveRootObject(_:toFile:) on the NSKeyedArchiver class.
func saveChanges() -> Bool { print("Saving items to: \(itemArchiveURL.path!)") return NSKeyedArchiver.archiveRootObject(allItems, toFile: itemArchiveURL.path!) }
The archiveRootObject(_:toFile:) method takes care of saving every single Item in allItems to the itemArchiveURL ...
Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.