tags:

views:

22

answers:

1

I am just curious if reusing a variable assigned to a convenience method is ok.

NSDictionary *address = [NSDictionary dictionaryWithObjectsAndKeys:@"Italy", @"Country", 
address = [NSDictionary dictionaryWithObjectsAndKeys:@"England", @"Country", nil];

or should I just assign a 2nd new variable?

NSDictionary *address = [NSDictionary dictionaryWithObjectsAndKeys:@"Italy", @"Country", 
NSDictionary *address2 = [NSDictionary dictionaryWithObjectsAndKeys:@"England", @"Country", nil];

cheers gary

+2  A: 

The first example will make the address pointer point to a different object, so you'll lose your reference to the original dictionary.

Because they're autoreleased, it won't leak memory, but you probably want to be able to access both dictionaries in the future. The first example won't do that for you, and you'll lose the first dictionary.

The second example is much better, it allows you reference both dictionaries independently without stepping on one or the other.

However, if you're looking to merge the two dictionaries, you should create a mutable copy of the first dictionary and then use addEntriesFromDictionary: to merge the two. Don't forget to release/autorelease the mutable copy you create when doing this.

Jasarien
Thank you, I was aware that I would not be able to access the first, but was unsure if reusing the pointer would have any impact on the memory getting released, thanks again for clearing that up.
fuzzygoat