26
Property Lists

Sometimes you need a file format that can be read by both computers and people. For example, let’s say that you want to keep a description of your stock portfolio in a file. As you add new stocks, it would be nice to be able to edit that file easily by hand. But, it might also be handy for one of your programs to be able to read it. When facing this problem, most Objective-C programmers use a property list.

A property list is a combination of any of the following things:

  • NSArray

  • NSDictionary

  • NSString

  • NSData

  • NSDate

  • NSNumber (integer, float or Boolean)

For example, an array of dictionaries with string keys and date objects is a property list (or just a P-list).

Reading and writing a property list to a file is really easy. In Xcode, create a new project: a Foundation Command Line Tool named stockz and add the following code:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​s​t​o​c​k​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​*​s​t​o​c​k​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​t​o​c​k​ ​=​ ​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​d​i​c​t​i​o​n​a​r​y​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​@​"​A​A​P​L​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​y​m​b​o​l​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​[​N​S​N​u​m​b​e​r​ ​n​u​m​b​e​r​W​i​t​h​I​n​t​:​2​0​0​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​h​a​r​e​s​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​a​d​d​O​b​j​e​c​t​:​s​t​o​c​k​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​t​o​c​k​ ​=​ ​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​d​i​c​t​i​o​n​a​r​y​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​@​"​G​O​O​G​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​y​m​b​o​l​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​[​N​S​N​u​m​b​e​r​ ​n​u​m​b​e​r​W​i​t​h​I​n​t​:​1​6​0​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​h​a​r​e​s​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​a​d​d​O​b​j​e​c​t​:​s​t​o​c​k​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​w​r​i​t​e​T​o​F​i​l​e​:​@​"​/​t​m​p​/​s​t​o​c​k​s​.​p​l​i​s​t​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​t​o​m​i​c​a​l​l​y​:​Y​E​S​]​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

(Notice that I reused the stock pointer. I used it to point to the first dictionary and then to the second.)

Figure 26.1  An array of dictionaries

An array of dictionaries

When you run the program, you’ll get a file stocks.plist. If you open it in a text editor, it looks like this:

<​?​x​m​l​ ​v​e​r​s​i​o​n​=​"​1​.​0​"​ ​e​n​c​o​d​i​n​g​=​"​U​T​F​-​8​"​?​>​
<​!​D​O​C​T​Y​P​E​ ​p​l​i​s​t​ ​P​U​B​L​I​C​
 ​ ​"​-​/​/​A​p​p​l​e​/​/​D​T​D​ ​P​L​I​S​T​ ​1​.​0​/​/​E​N​"​ ​"​h​t​t​p​:​/​/​w​w​w​.​a​p​p​l​e​.​c​o​m​/​D​T​D​s​/​P​r​o​p​e​r​t​y​L​i​s​t​-​1​.​0​.​d​t​d​"​>​
<​p​l​i​s​t​ ​v​e​r​s​i​o​n​=​"​1​.​0​"​>​
<​a​r​r​a​y​>​
 ​ ​ ​ ​<​d​i​c​t​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​k​e​y​>​s​h​a​r​e​s​<​/​k​e​y​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​i​n​t​e​g​e​r​>​2​0​0​<​/​i​n​t​e​g​e​r​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​k​e​y​>​s​y​m​b​o​l​<​/​k​e​y​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​s​t​r​i​n​g​>​A​A​P​L​<​/​s​t​r​i​n​g​>​
 ​ ​ ​ ​<​/​d​i​c​t​>​
 ​ ​ ​ ​<​d​i​c​t​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​k​e​y​>​s​h​a​r​e​s​<​/​k​e​y​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​i​n​t​e​g​e​r​>​1​6​0​<​/​i​n​t​e​g​e​r​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​k​e​y​>​s​y​m​b​o​l​<​/​k​e​y​>​
 ​ ​ ​ ​ ​ ​ ​ ​<​s​t​r​i​n​g​>​G​O​O​G​<​/​s​t​r​i​n​g​>​
 ​ ​ ​ ​<​/​d​i​c​t​>​
<​/​a​r​r​a​y​>​
<​/​p​l​i​s​t​>​

Nice, eh? Human-readable. XML. One line of code.

If you find yourself creating property lists by hand, you should know that Xcode has a built-in editor specifically for property lists.

Now add the code that reads the file in:

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​s​t​o​c​k​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​*​s​t​o​c​k​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​t​o​c​k​ ​=​ ​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​d​i​c​t​i​o​n​a​r​y​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​@​"​A​A​P​L​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​y​m​b​o​l​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​[​N​S​N​u​m​b​e​r​ ​n​u​m​b​e​r​W​i​t​h​I​n​t​:​2​0​0​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​h​a​r​e​s​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​a​d​d​O​b​j​e​c​t​:​s​t​o​c​k​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​t​o​c​k​ ​=​ ​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​d​i​c​t​i​o​n​a​r​y​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​@​"​G​O​O​G​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​y​m​b​o​l​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​ ​s​e​t​O​b​j​e​c​t​:​[​N​S​N​u​m​b​e​r​ ​n​u​m​b​e​r​W​i​t​h​I​n​t​:​1​6​0​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​K​e​y​:​@​"​s​h​a​r​e​s​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​a​d​d​O​b​j​e​c​t​:​s​t​o​c​k​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​s​t​o​c​k​s​ ​w​r​i​t​e​T​o​F​i​l​e​:​@​"​/​t​m​p​/​s​t​o​c​k​s​.​p​l​i​s​t​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​t​o​m​i​c​a​l​l​y​:​Y​E​S​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​s​t​o​c​k​L​i​s​t​ ​=​ ​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​C​o​n​t​e​n​t​s​O​f​F​i​l​e​:​@​"​/​t​m​p​/​s​t​o​c​k​s​.​p​l​i​s​t​"​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​d​ ​i​n​ ​s​t​o​c​k​L​i​s​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​I​ ​h​a​v​e​ ​%​@​ ​s​h​a​r​e​s​ ​o​f​ ​%​@​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​d​ ​o​b​j​e​c​t​F​o​r​K​e​y​:​@​"​s​h​a​r​e​s​"​]​,​ ​[​d​ ​o​b​j​e​c​t​F​o​r​K​e​y​:​@​"​s​y​m​b​o​l​"​]​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program.

Challenge

Write a tool that creates a property list that has all 8 types in it: array, dictionary, string, data, date, integer, float, boolean.

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

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