Here\'s a NSCoding compliant object. I would like to save and recover it from the app\'s documents directory in Swift 3. I imagine it\'s a save method and recover method. Ho
Saving:
// Get documents directory
if let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
// Append your file name to the directory path
let path = (docs as NSString).appendingPathComponent("filename")
// Archive your object to a file at that path
NSKeyedArchiver.archiveRootObject(yourObject, toFile: path)
}
Loading:
// Get documents directory
if let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
// Append your file name to the directory path
let path = (docs as NSString).appendingPathComponent("filename")
// Unarchive your object from the file
let yourObject = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? Book
// do whatever with yourObject
}