With a plist containing an array of arrays of dictionaries, how do you add and write out new dictionary elements
I have created a plist that is initialized as an array of arrays of dictionaries. The plist stores dynamic data. Consequently, when the app terminates, there may be new dictionary elements to add. Or there may be new arrays of dictionary elements to add. Later, there may be less dictionary elements so that previous elements are no longer required. While there is a lot of discussion on adding elements to a plist, none 开发者_如何学编程seem to address my problem. Here is the initial plist description:
Key Type Value
Root Dictionary (2 items)
Hourly Data Array (1 item)
Item 0 Array (1 item) <--how do I add more of these
Item 0 Dictionary (3 items) <--how do I add more of these
Name String Joe
Rank String private
serialNo String 123456
NonHourly Data Array (1 item)
Item 0 Array (1 item)
Item 0 Dictionary (3 items)
Name String Jeff
Rank String private
serialNo String 654321
While I fully understand how to read in this plist file, I do not understand how to add array elements to the Hourly and NonHourly Data arrays, or how to add new Dictionary array items, and then write them back out to the plist.
So given the following code as a starting point, how do I complete this code to add the array elements and dictionary elements described above:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *configPlist = [documentsDirectory stringByAppendingPathComponent:@"config.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: configPlist];
...
[data writeToFile: configPlist atomically:YES];
ok, after trial an error, I figure out how to do this. First, let me provide a definition to the data being stored. The data is for all army persons who have hourly and non-hourly status. The "Hourly Data" and "Non-Hourly Data" are considered the level 1 arrays. The elements of the level 1 arrays are referred to as level 2 arrays. The level 2 arrays can be thought of as the country the army persons have served in. The level 2 arrays have elements that are dictionaries. The dictionaries each describe an armyPerson.
Now for the answer, basically I need to work from the inside out and create an level 2 array of dictionaries that contain the data to store. That level 2 array is then added to a level 1 array. Finally, the level 1 array is stored as an object in Hourly Data or the Non-Hourly Data keys of the plist. Here is the solution:
// Create an array of arrays of content encoded as dictionary objects for hourly data
armyPerson *aPerson;
NSArray *level1Element;
NSMutableArray *level2Array;
NSMutableArray *level1Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];
for (int i=0; i<[allHourlyPersons count]; i++) {
level1Element = [allHourlyPersons objectAtIndex:i]; // grab the next level1 array element.
// Each level1 array element will have zero or more level2Arrays. The info for each level2 dictionary needs to be collected into
// an level2Array element as individual dictionary objects
level2Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease]; // initialize the level2Array
for (int j=0; j<[level1Element count]; j++) {
aPerson = [level1Element objectAtIndex:j]; // grab the next armyPerson
// For each episode, create a dictionary object
NSDictionary *level2Element = [NSDictionary dictionaryWithObjectsAndKeys:aPerson.name, @"Name",
aPerson.rank, @"Rank",
aPerson.serialNo, @"serialNo", nil];
[level2Array addObject:level2Element]; // save the info for this episode
}
// Now that we have all the episodes for this level1Element collected into an array of dictionary objects,
// we need to add this array to the level1 array
[level1Array addObject:level2Array];
}
// Finally, we need to create the key-value pair for the hourly source array
[data setObject:level1Array forKey:@"Hourly Data"];
...
Do the same for the Non-Hourly Data prior to:
[data writeToFile: configPlist atomically:Yes];
精彩评论