2016-09-24 57 views
0

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

我有一个NSMutableArray它有twoNSDictionary与密钥和值,我需要在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"]]; 
     } 
    } 
} 

mutdictNSMutableDictionary全局声明并实例化viewdidLoad

mutdict = [[NSMutableDictionary alloc] init]; 

因为我需要的值插入mutdict。相应地,每个SubAssetClassName被添加到AssetclassName中。

但我的问题是在我的最终字典,即mutdictSubAssetClassName的值重复。

有人可以告诉如何解决这个问题。

我的控制台

"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

您需要在循环内创建一个新的mutable字典实例。现在你创建一个实例并反复更新它。这导致一个字典被反复添加。

更改您的代码如下:

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

+0

谢谢你....在我的UITableView单元格中的一个更多的问题我得到这些值像'NSDictionary * rowData = [mutdict valueForKeyPath:[sectionArray objectAtIndex:indexPath.section]];'但不知道如何用SubAssetClass名称和值填充每个单元格。你能帮忙吗? –

+0

如果您还有其他问题,请发布新问题。一定要在问题中包含所有相关细节。 – rmaddy

+0

好吧,你说我问另一个问题,你可以检查在[链接](http://stackoverflow.com/questions/39680845/populate-nsmutabledictionary-value-to-uitableviewcell) –