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. In main.m, add the following code:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *stocks = [[NSMutableArray alloc] init]; NSMutableDictionary *stock; stock = [NSMutableDictionary dictionary]; [stock setObject:@"AAPL" forKey:@"symbol"]; [stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"]; [stocks addObject:stock]; stock = [NSMutableDictionary dictionary]; [stock setObject:@"GOOG" forKey:@"symbol"]; [stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"]; [stocks addObject:stock]; [stocks writeToFile:@"/tmp/stocks.plist" atomically:YES]; } return 0; }
(Notice that you reuse the stock pointer. You use it to point to the first dictionary and then to the second.)
When you run the program, you will get a file: stocks.plist. If you open it in a text editor, it looks like this:
<?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>shares</key> <integer>200</integer> <key>symbol</key> <string>AAPL</string> </dict> <dict> <key>shares</key> <integer>160</integer> <key>symbol</key> <string>GOOG</string> </dict> </array> </plist>
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:
int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *stocks = [[NSMutableArray alloc] init]; NSMutableDictionary *stock; stock = [NSMutableDictionary dictionary]; [stock setObject:@"AAPL" forKey:@"symbol"]; [stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"]; [stocks addObject:stock]; stock = [NSMutableDictionary dictionary]; [stock setObject:@"GOOG" forKey:@"symbol"]; [stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"]; [stocks addObject:stock]; [stocks writeToFile:@"/tmp/stocks.plist" atomically:YES]; NSArray *stockList = [NSArray arrayWithContentsOfFile:@"/tmp/stocks.plist"]; for (NSDictionary *d in stockList) { NSLog(@"I have %@ shares of %@", [d objectForKey:@"shares"], [d objectForKey:@"symbol"]); } } return 0; }
Build and run the program.