Raphael P. Raphael P. -4 years ago 86
iOS Question

iOS - How to fill an NSMutableDictionary with multiple keys in a switch

I am struggling in a situation that I can't understand.

Here is my code :

for (CDDetailDpt* detailDpt in [((CDGlobalInfo*)object).details allObjects]) {
NSMutableArray *array = [NSMutableArray array];
[array addObject:detailDpt.numDept];

NSLog(@"Department Number = %@", detailDpt.numDept);
NSLog(@"Color Number = %@", detailDpt.color);

switch ([detailDpt.color intValue]) {
case 2:
[_deptDict setValue:array forKey:detailDpt.color];
break;
case 3:
[_deptDict setValue:array forKey:detailDpt.color];
break;
case 4:
[_deptDict setValue:array forKey:detailDpt.color];
break;
default:
[_deptDict setValue:array forKey:detailDpt.color];
break;
}
}

for (id key in _deptDict)
NSLog(@"dept Dict = %@", [_deptDict objectForKey:key]);


Let me explain :

I am looping threw CoreData objects, and more precisely, I have a list of informations for each department of a Country.
In this Detail Object I have the departmentNumber, departmentName, and a colorNumber (going from 1 to 4, in order to display it on a map).

I want to loop on each department, and according to the color value (going from 1 to 4) I want to fill an NSDictionary with the associated department where the key are the color number.

For example :

My Dictionary will contains n department number for the key @"1" (color number = 1) etc... for each keys.

My log is only displaying me 1 value. I fill that I am not filling the dictionary.

I tried with setValue, setObject.. I don't see what I am doing wrong..

EDIT :

I have added an Array in the for loop in order to temporary store the value to not override it.

I feel like my Switch is useless, how can I add my array of department number according to a specific color department to the right key in my Dictionary ?

Moreover I also add some log so that you can see that I actually do have some values.

Log :

Department Number = 14
Color Number = 2
Department Number = 41
Color Number = 2
Department Number = 59
Color Number = 2
Department Number = 89
Color Number = 3
Department Number = 79
Color Number = 3
Department Number = 27
Color Number = 3
Department Number = 36
Color Number = 3

Answer Source

Your current code initializes the array for each element in the source, so that you wind up with only one element in each.

It works if you addObject: to the array, if the array already exists in the dictionary.

Like:

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

for (DetailDpt *detailDpt in self.detailDpts) {
    NSString *key = detailDpt.color.stringValue;
    if ([deptDict valueForKey:key]) {
        [(NSMutableArray *)[deptDict valueForKey:key] addObject:detailDpt.numDept];
    } else {
        [deptDict setValue:[NSMutableArray arrayWithObject:detailDpt.numDept] forKey:key];
    }
}

NSLog(@"deptDict = %@", deptDict);

...returns:

deptDict = {
   2 =     (
      14,
      41,
      89
   );
   3 =     (
       79,
       27,
       36
   );
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download