When adding items to NSMutableDictionary
using the setValue:forKey:
method (I suppose this generalizes to any NSObject
) does the dictionary retain the second parameter, the NSString
?
For example:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSString *theString = @"hello";
int i;
for (i=0; i<[theString length]; i++){
NSNumber *myInt = [NSNumber numberWithInt:i];
NSString *character = [NSString stringWithFormat:@"%C",[theString characterAtIndex:i]];
[dict setValue: myInt forKey:character];
}
[dict release];
[pool release];
Clearly, there is no reason to release myInt
in the loop, it is retained by dict
so it can't be released until the end of the code. But is the same true of character
? My thinking is that if NSMutableDictionary
stores the string in some other way, then one could create a temporary pool around the loop and release those strings instead of waiting until the release of the dictionary.
I am also curious as to why retainCount
of character
is 7fffffff as if it is an NSConstantString
, I would expect stringWithFormat
to return an NSString
object which would need retaining, but that doesn't seem to be the case.