Question

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 ?

Was it helpful?

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.

OTHER TIPS

The answer is no. Whether or not you are using ARC. No.

No, you do not need to retain the keys (or the values) of an NSDictionary. This is because NSDictionary copies the key. When you retrieve an objects with objectForKey: isEqual: is used to determine which key refers to the object you passed in.

The basic rule in manual memory management in Cocoa is -- worry about what you're doing in that object or method; don't worry about what any other object is doing.

All you're doing is passing the key to a method of the dictionary. You are not storing it around anywhere. Whatever the dictionary does with it, it is responsible for the proper memory management. What it does is none of your business.

(There is a slight exception with blocks, in that you sometimes must copy them before passing to a type-agnostic method. But let's not worry about this for now.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top