<分区>
我有一个名为 animals 的 NSMutableArray
。我需要创建一个 NSMutableDictionary
,以便动物数组中的所有名称都具有以特定首字母 = Values
开头的 Keys
。
这是动物数组:
NSMutableArray *animals = [NSMutableArray arrayWithObjects:@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu", @"Giraffe", @"Greater Rhea", @"Hippopotamus", @"Horse", @"Koala", @"Lion", @"Llama", @"Manatus", @"Meerkat", @"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear", @"Rhinoceros", @"Seagull", @"Tasmania Devil", @"Whale", @"Whale Shark", @"Wombat", nil];
这是我将其设置为 MutableDictionary
的代码:
for(NSString *str in animals) {
NSString *firstLetter = [str substringToIndex:1];
NSArray *newArr = [NSArray arrayWithObject:str];
[myMutableDictionary setValue:newArr forKey:firstLetter];
}
问题是每个键只设置了一个值,但我需要所有对象的第一个字母具有相同的值。例如。 value='b' -> @"Bear", @"Black Swan", @"Buffalo".