gpt4 book ai didi

ios - NSDictionary 获取重复值

转载 作者:行者123 更新时间:2023-11-29 00:40:17 24 4
gpt4 key购买 nike

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

我有一个 NSMutableArray,其中有 两个 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: 除非您有明确和具体的需要使用键值编码,而不是简单地从字典中获取给定键的对象。

关于ios - NSDictionary 获取重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39678479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com