Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Weitere Verwandte Inhalte

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Saving Data

  1. 1. Unit 4—Lesson 7: Saving Data
  2. 2. Saving data View Model User action Update Update Notify Controller
  3. 3. Encoding and decoding with Codable class Note: Codable {…} Use an Encoder object to encode Use a Decoder object to decode
  4. 4. Encoding Encoding and decoding with Codable struct Note: Codable { let title: String let text: String let timestamp: Date } let newNote = Note(title: "Dry cleaning", text: "Pick up suit from dry cleaners", timestamp: Date()) let propertyListEncoder = PropertyListEncoder() if let encodedNotes = try? propertyListEncoder.encode(newNote) { . . . }
  5. 5. Decoding Encoding and decoding with Codable let propertyListDecoder = PropertyListDecoder() if let decodedNote = try? propertyListDecoder.decode(Note.self, from: encodedNote) { . . . }
  6. 6. App sandbox
  7. 7. App sandbox
  8. 8. Sandboxing Writing data to a file App File Manager Documents Documents Documents
  9. 9. Documents directory Writing data to a file let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("appData").appendingPathExtension("plist")
  10. 10. Writing the data Writing data to a file let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("notes_data").appendingPathExtension("plist") let propertyListEncoder = PropertyListEncoder() let encodedData = try? propertyListEncoder.encode(data) try? encodedData?.write(to: archiveURL, options: .noFileProtection)
  11. 11. Reading the data Writing data to a file let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("appData").appendingPathExtension("plist") let propertyListDecoder = PropertyListDecoder() if let retrievedData = try? Data(contentsOf: archiveURL), let decodedNote = try? propertyListDecoder.decode(Note.self, from: retrievedNoteData) { . . . }
  12. 12. Saving an array of model data Writing data to a file let notes = [note1, note2, note3] let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("notes_data").appendingPathExtension("plist") let propertyListEncoder = PropertyListEncoder() let encodedData = try? propertyListEncoder.encode(notes) try? encodedData?.write(to: archiveURL, options: .noFileProtection)
  13. 13. Reading an array of model data Writing data to a file let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let archiveURL = documentsDirectory.appendingPathComponent("notes_data").appendingPathExtension("plist") let propertyListDecoder = PropertyListDecoder() if let retrievedNotesData = try? Data(contentsOf: archiveURL), let decodedNotes = try? propertyListDecoder.decode(Array<Note>.self, from: retrievedNotesData) { ... }
  14. 14. Remember Your model objects should implement the Codable protocol. Reading and writing should happen in the model controller. Archive in the correct app delegate life-cycle events. For example: • When the app enters the background • When the app is terminated
  15. 15. Saving Data Unit 4—Lesson 7 Learn how to persist data using Codable, a protocol for saving files to your app’s Documents directory.
  16. 16. Lab: Remember Your Best Friends Unit 4—Lesson 7 Use the Codable protocol to persist information between launches of an app listing your best friends.
  17. 17. © 2017 Apple Inc. This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

×