A little understanding of objective-c Memory Management

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.