Search code examples
iosobjective-creleasensmutabledictionaryretain

IOS: Do i need to retain key in dictionary ios?


I have dictionary in which i m storing

1)keys = which is string attribute of an "SimpleObject" with 'assign' property

--2) value = "SimpleObject"

NSMutableDictionary retains the object so im releasing the object. the key is present in the same object. The key is string.

Now do i need to explicitly retain the string key before add it to dict ?


Solution

  • No, you do not need to explicitly retain the NSString, because your object already does that. Since the dictionary retains the object, the NSString is safe from being released prematurely.

    Moreover, NSDictionary makes a copy of your string key, as a precaution against using a mutable object as a dictionary key (which is very bad). That's why you are free to change that key inside the object as you please. Of course that would not change the placement of the object inside the dictionary.