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

发布于 2024-12-08 21:52:15 字数 694 浏览 0 评论 0原文

目前,我有一个 tableView,它是从以下名为 exerciseArray 的数组加载的:

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

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

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

在此处输入图像描述

新的 plist 应该仅有 ExerciseNamemusclename,但我需要以某种方式将两者合并。

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

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:

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2024-12-15 21:52:15

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

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];

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

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

[root writeToFile:docPlist atomically:YES];

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

编辑:为了向现有肌肉添加练习,只需执行以下操作:

// 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文