Serialization and deserialization
You may notice that the to-do item you put in is gone when you restart the app. Such an app is useless for the user. The app needs to store the to-do items somehow and reload them when it is opened the next time. There are different possibilities to implement this. We could use Core Data, serialize the data using NSCoding
, or use a third-party framework. In this book, we will write the date into a property list (plist). A plist has the advantage that it can be opened and altered with Xcode or any other editor.
The data model we implemented uses structs. Unfortunately, structs cannot be written to a plist. We have to convert the data into Any
arrays and String:Any
dictionaries. Add the following code to ToDoItemTests
:
func test_HasPlistDictionaryProperty() { let item = ToDoItem(title: "First") let dictionary = item.plistDict }
The static analyzer complains that there is no property with the name plistDict
. Let's add it. Open ToDoItem
and add the property...