Created
October 8, 2020 09:56
-
-
Save SlappyAUS/e0809ea34bea60696ec406abff60ba6e to your computer and use it in GitHub Desktop.
Property List #data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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