NSDictionary获取重复值

时间:2016-09-24 16:22:37

标签: ios objective-c uitableview nsmutabledictionary

我知道这可能是一个重复的问题,但我搜索了很多,但无法为我找到合适的答案。

我有一个NSMutableArraytwo 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值都相同,但实际上并非如此。

我该如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

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

按如下方式更改代码:

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:除非您明确且特定地需要使用键值编码,而不是简单地从字典中获取给定键的对象。