NSDictionary 获取重复值

NSDictionary Getting Repeated values

我知道这可能是一个重复的问题,但我在谷歌上搜索了很多,但找不到适合我的答案。

我有一个 NSMutableArray,它有 two NSDictionary 以及我需要在 UITableView 上填充的键和值。我已经检索到 dictionary 的值,我将使用

填充它
NSMutableArray *mutArray = [responseArray valueForKey:@"Table"];

我确实喜欢

NSMutableSet *names = [NSMutableSet set];
NSMutableArray *mutArray1 = [[NSMutableArray alloc] init];

for (id obj in mutArray) {

    NSString *destinationName = [obj valueForKey:@"AssetClassName"];

    if (![names containsObject:destinationName]) {

       [mutArray1 addObject:destinationName];
       [names addObject:destinationName];

    }
} 

因为值AssetClassName重复了。现在我在 mutArray1 中有三个值,我需要将其显示为 UITableView 部分。在 AssetClassName 下,我有一些数据确定该部分中的行。

为了检索我正在做的数据

for (int i = 0; i < [mutArray1 count]; i++) {

    NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *b = [[NSMutableDictionary alloc] init];

    for (NSDictionary *dict in mutArray) {

        if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) {

           [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"];
           [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"];
           [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"];
           [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"];

           [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]];

           [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]];
        }
    }
}  

mutdict 是全局声明的 NSMutableDictionary 并在 viewdidLoad

中实例化
mutdict = [[NSMutableDictionary alloc] init];

根据需要将值插入 mutdict。每个 SubAssetClassName 相应地添加到 AssetclassName 中。

但我的问题出在我最终的字典中,即 mutdict SubAssetClassName 的值重复了。

谁能告诉我如何解决这个问题。

我的主机

"AssetClassName" =     {

    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
};
"AssetClassName" =     {
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "EMERGING MARKETS EQUITIES";
        "ModelAllocationPercentage" = 10;
    };
};
"AssetClassName" =     {
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
};
}

在这里我可以看到每个部分的所有 SubAssetClass 值都相同,但实际上并非如此。

我该如何解决这个问题。

您需要在循环内创建可变字典的新实例。现在您创建一个实例并一遍又一遍地更新它。这导致一遍又一遍地添加一个字典。

更改您的代码如下:

for (NSInteger i = 0; i < [mutArray1 count]; i++) {
    NSMutableDictionary *b = [[NSMutableDictionary alloc] init];

    for (NSDictionary *dict in mutArray) {
        if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) {
           NSMutableDictionary *a = [[NSMutableDictionary alloc] init];

           [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"];
           [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"];
           [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"];
           [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"];

           [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]];

           [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]];
        }
    }
}  

此外,在大多数情况下,您不应使用 valueForKey:。使用 objectForKey: 除非你有明确和具体的需要使用键值编码而不是简单地从字典中获取给定键的对象。