2

I have a NSMutableArray named animals. I need to create an NSMutableDictionary such that all names in the animals array have Keys that start with a specific first letter = Values.

This is animals array :

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];

this is my code to set it into a MutableDictionary :

 for(NSString *str in animals) {
     NSString *firstLetter = [str substringToIndex:1];
     NSArray *newArr = [NSArray arrayWithObject:str];
     [myMutableDictionary setValue:newArr forKey:firstLetter];
 }

The problem is that for each key only one value is set, but i need all the objects have a first letter with the same value. E.g. value='b' -> @"Bear", @"Black Swan", @"Buffalo".

Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78
Sattar
  • 108
  • 4
  • 12
  • 1
    Just to be clear... You just need the names grouped by their first letter or you need their first letter for something like indexing in a tableview? – Alladinian Apr 20 '15 at 11:32
  • 1
    For starters [NSDictionary setValue:forKey] doesn't work like you think it does. Review the docs on that. It will set a value for exactly one key, matching exactly that value. You will need to use something like NSPredicate to find all the values matching and get a subset array which you can copy into an NSDictionary. – Cliff Ribaudo Apr 20 '15 at 11:40
  • @Alladinian like indexing in a tableview – Sattar Apr 20 '15 at 11:48

2 Answers2

6

Try

for (NSString *str in animals) {
    NSString *firstLetter = [str substringToIndex:1];
    if(!myMutableDictionary[firstLetter])
    {
        myMutableDictionary[firstLetter] = [NSMutableArray new];
    }
    NSMutableArray *arr = myMutableDictionary[firstLetter];
    [arr addObject:str];
}
Avt
  • 16,927
  • 4
  • 52
  • 72
0

Here are the code that satisfied your requirement.

Step 1 : Allocate NSMutableDictionary.

Step 2 : While parsing NSArray, check initial is there key available in dictionary or what. if available then that array from same key and add new element in array and update same key in dictionary. if not available then create new key in dictionary. here is code.

NSMutableDictionary* dictTemp = [[NSMutableDictionary alloc]init];

for (NSString *str in animals) {

    NSString *firstLetter = [str substringToIndex:1];

    if([[dictTemp allKeys] containsObject:firstLetter]){
        NSMutableArray* arrayInner = [NSMutableArray arrayWithArray:[dictTemp valueForKey:firstLetter]];

        [arrayInner addObject:str];

        [dictTemp setValue:arrayInner forKey:firstLetter];

    }
    else{

        NSMutableArray* arrayInner = [[NSMutableArray alloc] init];
        [arrayInner addObject:str];
        [dictTemp setValue:arrayInner forKey:firstLetter];

    }


}

NSLog(@"Output : %@",dictTemp);
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43