Andrew Lauer Barinov
Andrew Lauer Barinov

Reputation: 5754

Mutable Dictionary Keys not being set properly

I have an NSMutableDictionary that is a synthesized property of a class. When I initialize the class I add several key value pairs to the dictionary, I am unable to later retrieve them and get a null result.

Some code:

[self.sections setValue:@"first" forKey:@"Some_Key"];
// sections is the synthesized NSMutableDictionary property
NSLog(@"First: %@", [self.sections valueForKey:@"Some_Key"]);

I get this in the log: First: (null)

Is there something I am omitting here?

Upvotes: 0

Views: 665

Answers (1)

andyvn22
andyvn22

Reputation: 14824

I'm guessing your dictionary itself is nil. In your initialization, add [self setSections:[NSMutableDictionary dictionary]]. That way, you've got an empty dictionary awaiting new values.

(I'm guessing this because one can message nil, and it will return nil for anything--just like you're getting when you ask for your value back.)

Upvotes: 2

Related Questions