views:

99

answers:

2

I have an NSDictionary object that is populated by NSMutableStrings for its keys and objects. I have been able to change the key by changing the original NSMutableString with the setString: method. They key however remains the same regardless of the contents of the string used to set the key initially.

My question is, is the key protected from being changed meaning it will always be the same unless I remove it and add another to the dictionary?

Thanks.

+2  A: 

Try using NSMutableDictionary, instead.

Alex Reynolds
Thanks, but that's not really an issue for the way I'm using it. Should've said I've tried using both type and that in this case I'm using the mutableCopy copy method and then trying to change the resulting NSMutableDictionary. Sorry, my bad.
Mark Reid
+1  A: 

The keys are -copy'd when the items are set, so you can't changing it afterwards is useless.

Methods that add entries to dictionaries—whether as part of initialization (for all dictionaries) or during modification (for mutable dictionaries)—copy each key argument (keys must conform to the NSCopying protocol) and add the copies to the dictionary. Each corresponding value object receives a retain message to ensure that it won’t be deallocated before the dictionary is through with it.

You could use CFDictionary with kCFTypeDictionaryKeyCallBacks, or just replace the item:

id value = [dictionary objectWithKey:oldKey];
[dictionary setObject:value withKey:newKey];
[dictionary removeObjectForKey:oldKey];
KennyTM
So what your saying is that the key is immutable regardless of the mutability of the actual collection being mutable or immutable?
Mark Reid
@Mark: Yes. ___
KennyTM
Thanks for the help.
Mark Reid