johnSnow
johnSnow

Reputation: 25

NSDictionary returning nil

I have a NSMutableDictionary with a NSDictionary as the value and they key is set to a year.

So when I debug my code, my _codes variable, the NSMutableDictionary returns Printing description of self->_codes:

{
    2013 =     {
        Depth = "21.5";
        RValue = R59;
        year = 2013;
    };
    2014 =     {
        Depth = "22.25";
        RValue = R60;
        year = 2014;
    };
}

but when I try to access it via:

NSDictionary *test = [_codes objectForKey:@"2014"];

test is always nil.

Anybody have any idea why?

Upvotes: 0

Views: 407

Answers (2)

Andy Obusek
Andy Obusek

Reputation: 12842

The type of the key is a NSNumber, not a NSString

Try:

NSDictionary *test = [_codes objectForKey:@2014];

or, more succinctly and modern:

NSDictionary *test = _codes[@2014];

Upvotes: 1

Owen Hartnett
Owen Hartnett

Reputation: 5935

Try:

[_codes objectForKey:[NSNumber numberFromInt:2014]];

Upvotes: 0

Related Questions