我在 NSDictionary 中有如下数据:
Value : Football, Key: SPORT
Value : Cricket, Key: SPORT
Value : Fastrack, Key: PRODUCT/SERVICE
Value : Audi USA, Key: CARS
Value : BMW, Key: CARS
Value : Facebook, Key: PRODUCT/SERVICE
Value : TED, Key: WEBSITE
Value : National Geographic, Key: MEDIA/NEWS/PUBLISHING
Value : MyWebProduct, Key: WEBSITE
我想根据键对值进行分组。在这种情况下我需要做什么或其他合适的想法来实现这一点。我希望结果显示如下:
SPORT : Football, Cricket
CARS : Audi, BMW
...
任何明显的帮助......
最佳答案
由于您有多个对象分组在同一个键下,因此数组字典将是包含您的数据的合适结构。
NSMutableDictionary *dict = [ [ NSMutableDictionary alloc ] init ];
NSArray *myItems = [ [ NSArray alloc ] initWithObjects:@"item one", @"item two", nil ];
[ dict setObject:myItems forKey:@"group of items" ];
然后您可以使用以下方式访问该组
[dict objectForKey:@"group of items"]
关于iphone - 按键对 NSDictionary 进行排序/分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13468176/