我需要将两个 NSArray/NSDictionaries 组合在一起

发布于 2024-12-20 17:32:28 字数 1642 浏览 2 评论 0原文

我有两个 plist 文件,用作数据源在我的应用中创建 NSArrayNSDictionaries

我希望 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 技术交流群。

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

发布评论

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

评论(2

◇流星雨 2024-12-27 17:32:28

您显然知道如何迭代和创建字典,那么问题是什么?

迭代 exerciseArray 并向其每个词典添加练习描述。然而,这似乎效率极低,您可能应该重新考虑整个事情。为什么不以随时可用的格式将它们存储在磁盘上?

当项目的顺序很重要时使用数组。在这里似乎并不重要,那么为什么不将它们保留为 NSDictionaries 呢?如果您需要访问字典中的所有键,只需调用 allKeys

编辑:像这样?

NSString *path = [[NSBundle mainBundle] pathForResource:@"ExerciseDescriptions"
    ofType:@"plist"];
NSDictionary *descriptions = [NSDictionary dictionaryWithContentsOfFile:path];

NSMutableArray *exercises = self.exerciseArray;
for (NSInteger i = 0; i < [exercises count]; i++) {

    NSMutableDictionary *dict = [[exercises objectAtIndex:i] mutableCopy];
    NSString *name = [dict valueForKey:@"exerciseName"];
    NSString *desc = [descriptions valueForKey:name];

    if (desc) {
        [dict setValue:desc forKey:@"exerciseDescription"];
        [exercises replaceObjectAtIndex:i withObject:dict];
    }
    [dict release];
}

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?

NSString *path = [[NSBundle mainBundle] pathForResource:@"ExerciseDescriptions"
    ofType:@"plist"];
NSDictionary *descriptions = [NSDictionary dictionaryWithContentsOfFile:path];

NSMutableArray *exercises = self.exerciseArray;
for (NSInteger i = 0; i < [exercises count]; i++) {

    NSMutableDictionary *dict = [[exercises objectAtIndex:i] mutableCopy];
    NSString *name = [dict valueForKey:@"exerciseName"];
    NSString *desc = [descriptions valueForKey:name];

    if (desc) {
        [dict setValue:desc forKey:@"exerciseDescription"];
        [exercises replaceObjectAtIndex:i withObject:dict];
    }
    [dict release];
}
予囚 2024-12-27 17:32:28

我创建了以下项目来解决合并从 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.).

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