Here is what i have in my main method:
int main (int argc, const char * argv[]) {
NSString *string=[[NSString alloc]init];
NSLog(@"%@ The retain count is:%d", string, [string retainCount]);
NSLog(@"Hello, World!");
return 0;
}
The output on executing this piece of code is “The retain count is :-1”.
I was expecting it to give me 1 as the retain count, since i created the object using alloc.
I have similar problems when i try to retain or release the NSString objects.
When i try with NSArray, i get the retain count immediately after creation of object as 2. I do not understand the mechanism of how the objects are being treated.
Please explain!
Thanks in advance..
-1 is what you get if you cast
UINT_MAXto a signed value.UINT_MAXis the documented retain count of objects which are never released, which includes string literal objects.Speaking of the documentation, I suggest reading the big box about how retainCount is generally not useful.