开发者

Need advice for merging two plists into an array

Currently, I have a tableView which is loaded from the following array called exerciseArray:

if (exerciseArray == nil)
    {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
        NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
        self.exerciseArray = rootLevel;
        [rootLevel release];
    }

However, I want an option to let the user add their own data which will ne开发者_StackOverflow社区ed to be displayed in this same table view. I can either add that data to the original plist but I think it will be better to create a separate plist that will have the user added data.

So after the user adds data, the I will need to combine the 2 plist data next time. Any idea how to implement this? The user added data will need to match up with original plist structure which looks like:

Need advice for merging two plists into an array

The new plist should only have ExerciseName and musclename, but I need to merge the two somehow.


The user-defined Property List should be stored in a file somewhere in the Documents directory. Usually, for something like this, I would suggest creating a copy of the default Plist that ships with the app to the Documents directory, then modifying and loading this file as needed. Copying the resource to the documents directory can be done as follows:

NSString * documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString * docPlist = [documentsPath stringByAppendingPathComponent:@"data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:docPlist]) {
    NSString * resPlist = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    [[NSFileManager] defaultManager] copyItemAtPath:resPlist
                                             toPath:docPlist
                                              error:nil];
}
NSMutableArray * root = [[NSMutableArray alloc] initWithContentsOfFile:docPlist];

The above code simply finds the path to the plist in the documents directory, and checks if it exists. If it does not, it copies the data.plist resource over to the documents directory. Once this is done, it loads the root element into a mutable array, which you can then use and modify using the standard addObject: and removeObject: methods.

Finally, to save any modified data, simply write the root array back to the plist file as follows:

[root writeToFile:docPlist atomically:YES];

The only delemma with this is, while in the process of developing the app, you may wish to change or modify some data in the data.plist resource. In order for the app to copy this newly modified Property List to the documents directory, you will need to uninstall it completely from the iOS Simulator, then re-compile and run.

Edit: In order to add an exercise to an existing muscle, just do something like this:

// find the exercise dictionary
int muscleIndex = 0;
NSMutableDictionary * muscleDict = nil;
NSString * muscleName = @"Neck";
NSString * exerciseName = @"Nod your head a billion times";
// find the right muscle
for (int i = 0; i < [root count]; i++) {
    NSDictionary * muscle = [root objectAtIndex:i];
    if ([[muscle objectForKey:@"muscleName"] isEqualToString:muscleName]) {
        muscleIndex = i;
        muscleDict = [NSMutableDictionary dictionaryWithDictionary:muscle];
    }
}

NSMutableArray * exercises = [NSMutableArray arrayWithArray:[muscleDict objectForKey:@"exercises"]];
[exercises addObject:exerciseName];
[muscleDict setObject:exercises forKey:@"exercises"];
[root replaceObjectAtIndex:muscleIndex withObject:muscleDict];
...
// save root here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜