To add to the messages of Joshua and Preston, actually [NSString stringWithString:xxx] returns xxx itself when xxx is a literal. This is an implementation detail, so you should not write any program relying on this fact, but it is interesting to know.
You can verify this fact in this way:
NSString* a=@ "foo"; NSString*b=[NSString stringWithString:a]; NSLog(@"a at %p, class %@",a,[a class]); NSLog(@"b at %p, class %@",b,[b class]);
At least on my box 10.6.3, both pointers have the same address as the NSCFString class.
Remember: retain and release are your responsibility for the property, and they do not always decrease / increase the number of deductions. An implementation can perform any optimization if this optimization does not violate the ownership policy.
Or, in other words: write retain and release so that the objects are saved / destroyed when the implementation always makes a naive increase / decrease in the number of deductions. This is a contract between Cocoa and you. But Cocoa can and does do a lot of optimization.
source share