Reading and Writing to plistsMarch 16, 2011 11:56 pm Posted By: bytedissident
Property lists otherwise referred to as plists are great mechanisms for storing small to moderate amounts of persistent data in iOS. You can store and retrieve right from Arrays and Dictionaries one line so it is very convenient.
First lets take a look a reading from a plist file. This is really a simple process of below is a step by step.
Step 1:
Determine the path of your file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"shows.plist"];
Step 2:
Read your file into an Array or with the second line read it into a Dictionary.
NSArray *playlist = [[NSArray arrayWithContentsOfFile:path] retain];
OR
NSDictionary *playlist = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
Writing to a plist:
Step 1:
Determine the path to the file you want to write to;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path2 = [documentsDirectory stringByAppendingPathComponent:@"games.plist"];
Step 2:
With either your Array your Dictionary write your plist.
[games writeToFile:path2 atomically:YES];
|