Thanks Steve.
I was under the understanding that an NSString object was always a Constant Character String but after some testing I've realized that is not the case.
As in the following code example.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *str1 = @"String One";
NSString *str2 = [NSString stringWithCString: (char *)"hello"];
NSString *str3 = [[NSString alloc] initWithString: @"String 3"];
NSString *str4 = [NSString stringWithFormat: @"%s and %i", (char *)"Happy New Year ", 2012];
NSLog(@"str1: Retain Count: %lx", [str1 retainCount]);
NSLog(@"str2: Retain Count: %lx", [str2 retainCount]);
NSLog(@"str3: Retain Count: %lx", [str3 retainCount]);
NSLog(@"str4: Retain Count: %lx", [str4 retainCount]);
[pool drain];
return 0;
}
Produces the following output:
[Session started at 2012-01-06 15:02:51 -0600.]
2012-01-06 15:02:51.537 Practice[4069:10b] str1: Retain Count: 7fffffff
2012-01-06 15:02:51.559 Practice[4069:10b] str2: Retain Count: 1
2012-01-06 15:02:51.568 Practice[4069:10b] str3: Retain Count: 7fffffff
2012-01-06 15:02:51.571 Practice[4069:10b] str4: Retain Count: 1
So depending how the NSString object was created will determine whether or not it is treated as a Constant Character String Object.
It is interesting to note that even though str3 was created using
alloc and
init that it still doesn't need to be released.
Thanks for the help. Great book by the way.