Skip to content

Instantly share code, notes, and snippets.

@nathanclark
Created May 20, 2011 16:37
Show Gist options
  • Select an option

  • Save nathanclark/983288 to your computer and use it in GitHub Desktop.

Select an option

Save nathanclark/983288 to your computer and use it in GitHub Desktop.
Creating a property list programmatically
NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:2];
NSDictionary *innerDict;
NSString *name;
NSDate *dob;
NSArray *scores;
scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:6],
[NSNumber numberWithFloat:4.6],
[NSNumber numberWithLong:6.0000034],
nil];
name = @"George Washington";
dob = [NSDate dateWithString:@"1732-02-17 04:32:00 +0300"];
innerDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: name, dob, scores, nil]
forKeys:[NSArray arrayWithObjects:@"Name", @"DOB", @"Scores"]];
//Adds the key value to the property list
[rootObj setObject:innerDict forKey:@"Washington"];
scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:8],
[NSNumber numberWithFloat:4.9],
[NSNumber numberWithLong:9.003433],
nil];
name = @"Abraham Lincoln";
dob = [NSDate dateWithString:@"1809-02-12 13:18:00 +0400"];
innerDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: name, dob, scores, nil]
forKeys:[NSArray arrayWithObjects:@"Name", @"DOB", @"Scores"]];
//Adds the key value to the property list
[rootObj setObject:innerDict forKey:@"Lincoln"];
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment