我知道這可能是一個重複的問題,但我GOOGLE了很多,但無法爲我找到合適的答案。NSDictionary獲取重複值
我有一個NSMutableArray
它有two
NSDictionary
與密鑰和值,我需要在UITableView
填充。我已經檢索到該我去填充使用
NSMutableArray *mutArray = [responseArray valueForKey:@"Table"];
的的dictionary
價值和我不喜歡
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
值相同的每個部分,但實際上它不是。
我該如何解決這個問題。
謝謝你....在我的UITableView單元格中的一個更多的問題我得到這些值像'NSDictionary * rowData = [mutdict valueForKeyPath:[sectionArray objectAtIndex:indexPath.section]];'但不知道如何用SubAssetClass名稱和值填充每個單元格。你能幫忙嗎? –
如果您還有其他問題,請發佈新問題。一定要在問題中包含所有相關細節。 – rmaddy
好吧,你說我問另一個問題,你可以檢查在[鏈接](http://stackoverflow.com/questions/39680845/populate-nsmutabledictionary-value-to-uitableviewcell) –