NSString * str1 = [NSString stringWithString: @ "str1"];
NSString * str2 = @ "str2 ";
NSString * str3 = [NSString stringWithFormat: @ "% @", @ "str3"];
NSString * str4 = [[NSString alloc] initWithString: @ "str4"];
What is the difference between the four?
I don't know what xuanjicang is available in str2, but what I'm sure is:
Str1 and str3 are all obtained by static methods of the NSString class. In this way, the returned NSString object will be autorelisted,
Specifically, when you use such an NSString object, you do not need to manually recycle it.
Str4 is different. str4 is generated by calling the object method after NSString alloc has an object.
In this way, the NSString object is not autorelease, and must be manually released after use, otherwise it will cause leakage!
The same class is included in cocos2d:
CCSprite
For example:
CCSprite * sprite1 = [[CCSprite alloc] initWithFile: @ "1.png"];
CCSprite * sprite2 = [CCSprite spriteWithFile: @ "2.png"];
And NSString are easy to use. You can be certain that this policy is certainly widely used in the basic objective-c framework!
From yang3wei's column