Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
Created October 8, 2020 09:56
Show Gist options
  • Select an option

  • Save SlappyAUS/e0809ea34bea60696ec406abff60ba6e to your computer and use it in GitHub Desktop.

Select an option

Save SlappyAUS/e0809ea34bea60696ec406abff60ba6e to your computer and use it in GitHub Desktop.
Property List #data
// Mark "Item" struct as Codable
var itemArray: [Item] = []
let dataFilePath =
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?
.appendingPathComponent("Items.plist")
func loadModel() {
if let data = try? Data(contentsOf: dataFilePath!) {
let decoder = PropertyListDecoder()
do {
itemArray = try decoder.decode([Item].self, from: data)
}
catch {
print("Error decoding: \(error)")
}
}
}
func saveModel() {
let encoder = PropertyListEncoder()
do {
let itemData = try encoder.encode(self.itemArray)
try itemData.write(to: self.dataFilePath!)
} catch {
print("Error encoding item array: \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment