Beginning iOS Development

Course No. 10-152-143

Chapter 26 — Property Lists (Obj-C)

  • We have saved a string to the file system before. It worked like this.
  • What would happen if we saved an array of strings? Let’s find out! (Could it be as simple as the same way we saved a string?)
  • Yes! It was actually even simpler, there was no error object and encoding to deal with. So, how did it get saved? Here’s how.
  • It got saved as a special XML document called a Property List, or plist for short.
  • A plist can save lots of different kinds of data types.
    • NSArray
    • NSDictionary
    • NSString
    • NSData
    • NSNumber
  • You can nest all of these in one plist. You could have an array of dictionaries, for instance. Here’s the example from the book.
  • It’s an array of dictionaries that contain keys and values.
  • Property Lists are very handy, iOS developers use them all the time for lots of purposes.

Creating plists

  • Since they are just XML they are easy to make. We can just open any text editor and write them. However, Xcode makes it easier than that.
  • When we create a new file with ⌘-N we can choose a Property List.

  • After we name and save it, we will see the plist editor.

  • We can choose an Array or a Dictionary as the root element of our document. I’ll choose Array for this demo.

  • Click on the little plus symbol button to add a new element.

  • We can set the data type for the new element to these types. I’ll choose Number

  • Then you enter the value you want for the element.

  • At any point you can use Xcode to view the XML source code of your plist file. Control-Click, or Right-Click the plist file name.

  • Select Open As→Source Code and you will see the true content of the file.

  • If you wanted to, you could make this a bigger list of integers with some text editing.
  • Then switch back to the Property List view.

Reading a plist

  • Now that we have a property list let’s read it back into a program.
  • That’s it! If we display the array we get this.
  • Yup, that’s an NSArray.