需要有关将两个plist合并到一个数组中的建议

时间:2021-12-12 12:32:28

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

目前,我有一个tableView,它从以下数组加载,名为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 need 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.

但是,我想要一个选项让用户添加自己的数据,这些数据需要在同一个表视图中显示。我可以将这些数据添加到原始plist中,但我认为最好创建一个单独的plist,让用户添加数据。

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:

因此,在用户添加数据之后,我将需要下次合并2个plist数据。知道如何实现这个吗?用户添加的数据需要与原始的plist结构匹配,如下所示:

需要有关将两个plist合并到一个数组中的建议

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

新的plist应该只有ExerciseName和musclename,但我需要以某种方式将两者合并。

1 个解决方案

#1


1  

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:

用户定义的属性列表应存储在Documents目录中的某个文件中。通常,对于这样的事情,我建议创建一个随应用程序一起提供到Documents目录的默认Plist的副本,然后根据需要修改和加载此文件。将资源复制到文档目录可以按如下方式完成:

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.

上面的代码只是找到文档目录中plist的路径,并检查它是否存在。如果没有,则将data.plist资源复制到文档目录。完成此操作后,它会将根元素加载到一个可变数组中,然后您可以使用标准的addObject:和removeObject:方法来使用和修改它。

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

最后,要保存任何已修改的数据,只需将根数组写回plist文件,如下所示:

[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.

唯一的解决方案是,在开发应用程序的过程中,您可能希望更改或修改data.plist资源中的某些数据。为了让应用程序将这个新修改的属性列表复制到文档目录,您需要从iOS模拟器中完全卸载它,然后重新编译并运行。

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

#1


1  

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:

用户定义的属性列表应存储在Documents目录中的某个文件中。通常,对于这样的事情,我建议创建一个随应用程序一起提供到Documents目录的默认Plist的副本,然后根据需要修改和加载此文件。将资源复制到文档目录可以按如下方式完成:

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.

上面的代码只是找到文档目录中plist的路径,并检查它是否存在。如果没有,则将data.plist资源复制到文档目录。完成此操作后,它会将根元素加载到一个可变数组中,然后您可以使用标准的addObject:和removeObject:方法来使用和修改它。

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

最后,要保存任何已修改的数据,只需将根数组写回plist文件,如下所示:

[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.

唯一的解决方案是,在开发应用程序的过程中,您可能希望更改或修改data.plist资源中的某些数据。为了让应用程序将这个新修改的属性列表复制到文档目录,您需要从iOS模拟器中完全卸载它,然后重新编译并运行。

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