Rahul Salvikar
Rahul Salvikar

Reputation: 3

Get the values from NSMutableDictionary inside NSMutableDictionary

I create two NSMutableDictionary: dictionary1 and dictionary2. In the first dictionary I store some array object having there keys. And in the second dictionary I store the object of first dictionary with the key like this:

int page = 1;
[dictionary2 setObject:dictionary1 forKey:[NSString stringWithFormat:@"%d",page]];
[dictionary1 removeAllObjects];

but at the time of retrieve of the object i do like this

dictionary1 = [dictionary2 valueForKey:[NSString stringWithFormat:@"%d",page]];
NSLog(@"dictionary1 %@", dictionary1);`

Then it gives null value. I don't know what mistake I do.

Upvotes: 0

Views: 100

Answers (3)

lnafziger
lnafziger

Reputation: 25740

After you add dictionary1 to dictionary2, you are removing all of the objects.

This is the same dictionary that is in dictionary2 (it does not create a copy), therefore you are removing the objects from it as well.

You need to remove the line [dictionary1 removeAllObjects];.

Then, since you are done with dictionary1 at this point, you can either remove the reference to it or set to a nice new shiny dictionary which is ready to use:

// Remove the reference
dictionary1 = nil;

// Or, create a new, empty dictionary
dictionary1 = [[NSDictionary alloc] init];

Upvotes: 4

Saad
Saad

Reputation: 8947

try this

   dictionary1 = [dictionary2 objectForKey:[NSString stringWithFormat:@"%d",page]];

Upvotes: 0

Mike Weller
Mike Weller

Reputation: 45598

Did you correctly alloc/init dictionary1 and dictionary2? Make sure the dictionaries themselves are not nil.

Upvotes: 0

Related Questions