To add to the posts by Joshua and Preston, in fact [NSString stringWithString:xxx]
returns xxx
itself when xxx
is a literal.
This is an implementation detail, so you shouldn't write any program relying on this fact, but it's fun to know.
You can check this fact thus:
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 10.6.3 box, both give the same address, with class NSCFString
.
Remember: retain
& release
concern your responsibility on the ownership, and they don't always decrease/increase the retain count. The implementation can do whatever optimization it wants, as long as the said optimization doesn't break the ownership policy.
Or in other words: write retain
& release
so that the objects are kept/destroyed in the case the implementation always does the naive increase/decrease of the retain count. That's the contract between Cocoa and you. But Cocoa can do and indeed does a lot of optimization.