需要有关将两个 plist 合并到数组中的建议
目前,我有一个 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 应该仅有 ExerciseName
和 musclename
,但我需要以某种方式将两者合并。
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:
The new plist should only have ExerciseName
and musclename
, but I need to merge the two somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用户定义的属性列表应存储在Documents目录中某处的文件中。通常,对于这样的事情,我建议创建随应用程序附带的默认 Plist 的副本到 Documents 目录,然后根据需要修改并加载此文件。将资源复制到文档目录可以如下完成:
上面的代码只是在文档目录中查找plist的路径,并检查它是否存在。如果没有,它会将 data.plist 资源复制到文档目录。完成此操作后,它将根元素加载到可变数组中,然后您可以使用标准
addObject:
和removeObject:
方法来使用和修改该数组。最后,要保存任何修改的数据,只需将
root
数组写回 plist 文件,如下所示:唯一的困境是,在开发应用程序的过程中,您可能希望更改或修改 data.plist 资源中的某些数据。为了让应用程序将这个新修改的属性列表复制到documents目录中,您需要从iOS模拟器中完全卸载它,然后重新编译并运行。
编辑:为了向现有肌肉添加练习,只需执行以下操作:
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:
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:
andremoveObject:
methods.Finally, to save any modified data, simply write the
root
array back to the plist file as follows: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: