Property Lists

A property list is a representation of data that can be saved to disk and read back in at a later point. Property lists can represent hierarchies of data and so are a great tool for saving and loading lightweight object graphs.

Under the hood, property list data can be represented by a number of formats, but you will frequently see them represented using an XML or binary format. Here is an example XML property list describing two items:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
            "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <array>
        <dict>
            <key>dateCreated</key>
            <date>2018-10-14T13:24:01Z</date>
            <key>name</key>
            <string>Fluffy Mac</string>
            <key>serialNumber</key>
            <string>96854567</string>
            <key>valueInDollars</key>
            <integer>62</integer>
        </dict>
        <dict>
            <key>dateCreated</key>
            <date>2018-10-15T19:47:01Z</date>
            <key>name</key>
            <string>Shiny Bear</string>
            <key>serialNumber</key>
            <string>36DDDB2B</string>
            <key>valueInDollars</key>
            <integer>91</integer>
        </dict>
    </array>
    </plist>

Property lists can hold the following types: Array, Bool, Data, Date, Dictionary, Float, Int, and String. As long as a given type is composed of those types, or a hierarchy of those types, then it can be represented as a property list.

Time to encode your items into a property list. In ItemStore.swift, implement a new method that will be responsible for saving the items.

Listing 13.2  Implementing saveChanges() to persist items (ItemStore.swift)

func saveChanges() -> Bool {

    let encoder = PropertyListEncoder()
    let data = encoder.encode(allItems)

    return false
}

(You will have an error, which we will discuss shortly.)

First, you create an instance of PropertyListEncoder. Then you call the encode(_:) method on that encoder, passing in whatever Codable type you would like to encode. Here, you pass in the allItems array, which will encode each of the Item instances. The encode(_:) method returns an instance of Data, which is a type that holds some number of bytes of binary data.

Encoding is a recursive process. When an instance is encoded (that is, when it is the first argument in encode(_:)), that instance is sent encode(to:). During the execution of its encode(to:) method, it encodes its properties using encode(_:forKey:). Thus, each instance encodes any properties that it references, which encode any properties that they reference, and so on (Figure 13.2).

Figure 13.2  Encoding an object

Encoding an object

So what about that error you saw? The error on the line that calls encode(_:) says: Call can throw, but it is not marked with 'try' and the error is not handled. This compiler error indicates that you are not handling the possibility of the encoding process failing. Let’s discuss how Swift approaches error handling and then use that knowledge to fix the compiler error that was just introduced.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.234.188