Module Outline
Use the following resources and work with your mentor to master the objectives for this module. Practice by completing the Mini-Project, Project, and Challenges.
Objectives
- You will be able to make simple model objects which conform to the Codable protocol
- You will be able to persist data between app launches using Codable and plists
- You will be able to use CodingKeys to customize mapping between properties and JSON keys
Persistent data is information that is written to disk or to a web service that can be accessed after closing and reopening your iOS application.
Common tools for local persistence include plists, UserDefaults, and Core Data. This lesson covers saving our data to a file on disk.
Required Preclass Resources
Lesson Video
Preclass Videos
-
iOS Cohort - Jake Gundersen
Serialization and Persistence
-
iOS Cohort - Jake Gundersen
Simple Persistence
-
iOS Cohort - Jake Gundersen
Codable Protocol Deeper Dive
Video Challenges
-
Add Properties
Add some extra properties to the model object. Try adding properties of different types. See if you can run into a type that doesn't conform to Codable.
-
Change from JSONEncoder to a PropertyListEncoder
After using the PropertyListEncoder to encode your model object, print out the encoded data string and compare the differences to the JSONEncoder string.
Stretch Problem
- Make a person class with name, age, and favoriteMovie properties
- init the class with a dictionary of type string:anyobject.
- Test this to make sure that it works
- Read about Failable Initializers
- Use a guard statement to handle missing dictionary values
- If a value is missing, don't init the person and instead return nil
Failable Initializers
Example Dictionary:
let workingDictionary = ["nameKey" : "Derek", "ageKey": 28, "favMovieKey" : "Zoolander"]
let brokenDictionary = ["nameKey" : "Steve", "ageKey": 2]
PART 1
PART 2
♦ Black Diamond
Guided Project
PlaylistCodable
PlaylistCodable is designed to introduce students to the Codable protocol.
Project
-
Journal
Students will build a simple Journal app to practice MVC separation, protocols, master-detail interfaces, table views, and persistence.
Journal is an excellent app to practice basic Cocoa Touch princples and design patterns. Students are encouraged to repeat building Journal regularly until the principles and patterns are internalized and the student can build Journal without a guide.
ReadMe
Mastery Review
- Failable Initializers stretch problem
- Challenges
- Add Properties objective challenge
- Change from JSONEncoder to a PropertyListEncoder objective challenge
- PlaylistCodable guided project