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
Here's the same design using guard statements.
import UIKit
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let book = Book(title: "Atlas Shrugged", author: "Ayn Rand", pageCount: 10, categories: ["Ethics"], available: true)
save(object: book, filename: "Bookshelf")
let atlasShrugged = retrieve(filename: "Bookshelf") as! Book
print("\(atlasShrugged.title) by \(atlasShrugged.author)")
}
func save(object:NSObject ,filename:String){
//build filepath in doc directory
guard let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
let path = (docs as NSString).appendingPathComponent(filename)
//save object to path in docs
NSKeyedArchiver.archiveRootObject(object, toFile: path)
}
func retrieve(filename:String) -> AnyObject? {
// Get documents directory
guard let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return nil }
//build path to file
let path = (docs as NSString).appendingPathComponent(filename)
//Unarchive from doc directory
let object = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? Book
return object
}
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
}