Today, I realized that initWithString method of NSString behaved differently.
NSString *myString = [[NSString alloc] initWithString:@"my string"]; NSLog( @"retain count = %u", [myString retainCount]; // 1 or 0xFFFFFFFF?
I’m pretty sure that it was 1 before. However, with current version of Xcode and its tool sets, it returns 0xFFFFFFFF.
However, if a string object is created with initWithFormat:, its retainCount is surely 1.
On tweeter, ChangKi Kim, aka MacCrazy, told me that it was changed due to optimization done by Apple, and it treats such immutable string like string literal like :
NSString *myString = @"my string";
The new behaviour is against Apple’s own object ownership rule. Also, initWithString: is definitely for creating an object in a heap not a stack. If a programmer wants to create one in stack, he can always use the string literal.
Why did Apple people break their rule and didn’t write any explanation about it?
When people like me teach Objective-C to others and if this kind of untold change comes up suddenly, students will think that the tutor is not good at Objective-C. Especially it is true here in U.S. Even when interviewed, I was surprised that they didn’t consider long experience and just want to hear “memorized” right answer when you know a lot more than them and you spend your time to figure out in what context they ask questions.
When people who have less variety of knowledge think about a given problem, things can look clear. However, people who have broader knowledge confronted that same question, he first needs to figure out in what context the question is asked. It is like to say that infinity + 1 equals infinity, while a chaos mathematician will answer infinity + 1 is bigger by 1 than infinity. Then the one who doesn’t know the contemporary mathematics thinks that the chaos mathematician is wrong or doesn’t know mathematics.
Anyway, Apple people should not deviate from the original and agreed-upon semantics.