PlaylistCodable is designed to introduce students to the codable protocol.
Look over the README in the root folder of this repository
Start this project from Playlist (the version without persistence), not from scratch
- Show students the final product first so they know what they are working toward.
- Take a few minutes to go over the version without persistence and make sure everyone understands things like
prepare(for segue: UIStoryboardSegue, sender: Any?)
. - Briefly talk about persisting data in general. Why an app would need it and some of the options we have for persisting data. Talk about the idea that most of the time you cannot store your custom objects "as-is". Explain how
Codable
solves this issue. - Explain that for this project we will be saving our data locally. Write a function called
fileURL()
which returns the url we will be saving our data to. - Plan out the new methods you will need to store and load data. (
saveToPersistentStore()
,loadFromPersistentStore()
) - Fill out your
saveToPersistentStore()
function using aJSONEncoder
. (Make sure you print out the data created by the encoder as string so that the students can see the json that is being created to hold the properties of the model objects) - Determine where in the project you need to call
saveToPersistentStore()
and add the call(s). Explain why we don't actually need adeleteFromPersistentStore()
function. How calling save at the right time instead will actually update the persistent store correctly. - Fill out your
loadFromPersistentStore()
function using aJSONDecoder
. - Determine where in the project to load your data, and call
loadFromPersistentStore()
. Everyone's app should work now.