Reputation: 233
Can someone please help me with what should be a pretty basic issue..
The Im trying to get the values stored in the dictionary via an array of objects (_options) with property section which is between 1 and 3
_sections = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @1, @"2", @2, @"3", @3, nil];
NSLog(@"sections=%lu",(unsigned long)[_sections count]);
for(Options *thisOption in _options)
{
NSString *key = [NSString stringWithFormat:@"%@", thisOption.section];
NSLog(@"key=%@",key);
NSNumber *count = [_sections objectForKey:key];
NSLog(@"count=%@",count);
}
my log displays sections=3, key=2 but count=(null)
Upvotes: 0
Views: 73
Reputation: 237050
Your key is the string @"2", but the key in the dictionary is the NSNumber 2. Those two objects aren't the same, so when you try to look up an object with that key, it doesn't find anything and returns nil.
Upvotes: 1
Reputation: 5656
You have the order of your objects and keys reversed in dictionaryWithObjectsAndKeys:.
I suggest using literals to make your code a bit more clear:
(Assuming your dictionary must be mutable)
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:@{@"1": @1, @"2": @2}];
(If you forget the mutable part)
NSDictionary *d = @{@"1": @1, @"2": @2};
Upvotes: 1