我需要将两个 NSArray/NSDictionaries 组合在一起
我有两个 plist
文件,用作数据源在我的应用中创建 NSArray
和 NSDictionaries
。
我希望 CSV 文件的输出如下所示:
exerciseName, muscleGroup, description
Barbell Curl, Biceps, This is a bicep exercise
问题是,我需要首先合并我拥有的两个 NSDictionaries。一个有exerciseName 和muscleName,而另一个有exerciseDescription。但我需要为每个具有所有 3 个键的练习对象准备一组字典。
我使用以下代码来构建主 NSMutableArray
if (muscleArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.muscleArray = rootLevel;
}
NSMutableArray *arrayForSearching = [NSMutableArray array];
for (NSDictionary *muscleDict in self.muscleArray)
for (NSDictionary *excerciseDict in [muscleDict objectForKey:@"exercises"])
[arrayForSearching addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[excerciseDict objectForKey:@"exerciseName"], @"exerciseName",
[muscleDict objectForKey:@"muscleName"], @"muscleName", nil]];
self.exerciseArray = arrayForSearching;
我使用以下代码来构建具有exerciseDescription 键的 NSDictionary
NSString *exerciseNameString =self.exerciseName;
NSString *path = [[NSBundle mainBundle] pathForResource:@"ExerciseDescriptions" ofType:@"plist"];
NSDictionary *exerciseDescription = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *description = [exerciseDescription objectForKey:exerciseNameString];
I have two plist
files that I'm using as datasources to create NSArray
and NSDictionaries
in my app.
I want the output of the CSV file to look like:
exerciseName, muscleGroup, description
Barbell Curl, Biceps, This is a bicep exercise
The problem is, I need to first combine two NSDictionaries I have. One has exerciseName and muscleName, while the other has exerciseDescription. But I need to have one array of dictionaries for each exercise obejct that has all 3 keys.
I'm using the following code to build the main NSMutableArray
if (muscleArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.muscleArray = rootLevel;
}
NSMutableArray *arrayForSearching = [NSMutableArray array];
for (NSDictionary *muscleDict in self.muscleArray)
for (NSDictionary *excerciseDict in [muscleDict objectForKey:@"exercises"])
[arrayForSearching addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[excerciseDict objectForKey:@"exerciseName"], @"exerciseName",
[muscleDict objectForKey:@"muscleName"], @"muscleName", nil]];
self.exerciseArray = arrayForSearching;
I'm using the following code to build the NSDictionary which has the exerciseDescription key
NSString *exerciseNameString =self.exerciseName;
NSString *path = [[NSBundle mainBundle] pathForResource:@"ExerciseDescriptions" ofType:@"plist"];
NSDictionary *exerciseDescription = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *description = [exerciseDescription objectForKey:exerciseNameString];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您显然知道如何迭代和创建字典,那么问题是什么?
迭代
exerciseArray
并向其每个词典添加练习描述。然而,这似乎效率极低,您可能应该重新考虑整个事情。为什么不以随时可用的格式将它们存储在磁盘上?当项目的顺序很重要时使用数组。在这里似乎并不重要,那么为什么不将它们保留为 NSDictionaries 呢?如果您需要访问字典中的所有键,只需调用
allKeys
编辑:像这样?
You obviously know how to iterate through and create dictionaries, so what's the problem?
Iterate through
exerciseArray
and add an exercise description to each of its dictionaries. However this seems wildly inefficient and you should probably rethink the whole thing. Why not store them on disk in a ready-to-use format?Arrays are used when the order of items matters. It doesn't seems to really matter here, so why not just keep them as NSDictionaries? If you need to access all keys in a dictionary just call
allKeys
EDIT: Like this?
我创建了以下项目来解决合并从 plist 创建的两个字典的需要:
https://github.com/bumboarder6/NSDictionary-merge
即使你有一些重复的内容它也能工作两个字典或数组之间的条目,它也会递归合并,这样你就可以合并整个 plist,即使它包含字典数组的字典的字典(等等)。
I created the following project to address my need to merge two dictionaries created from plists:
https://github.com/bumboarder6/NSDictionary-merge
It works even if you have some duplicate entries between your two dictionaries or arrays and it also recursively merges so you get a merge of your whole plist even when it contains dictionaries of dictionaries of arrays of dictionaries (etc.).