iOS Common string NSString Auto-Release comprehension

Source: Internet
Author: User
Tags vars

Recently in the process of doing, suddenly found that the understanding of nsstring is still problematic. Therefore, I would like to add a blog, I hope to have just entered the OC development and do not know the memory leakage problem of the people a little guidance.

Assignment value:

NSString * str = @ "123"; The meaning of this code is actually attached to a constant to STR, which is automatically managed by the system, do not need release releases, will be autorelease.

NSString * str = [[Nstring alloc] Initwithstring: @ "111"]; This writing itself is problematic because the meaning of this code is to attach a constant to STR, so it is optimized by the compiler and therefore does not need to be release, despite the presence of Alloc.

NSString * str = [[NSString alloc] initwithformat:@ "123"];//Must be released to be able, because this will lead to memory leaks.

NSString * str = [[NSString stringwithformat:@ "111"];//does not require release, nor does it produce a memory leak, because that part calls the system's class method, which is autorelease. This method is also referred to as a temporary variable use method.

Add a little bit of content:

1, Initwithformat is an example method

Can only be called by nsstring* str = [[NSString alloc] initwithformat:@ "%@", @ "Hello World"], but must be manually released to free memory resources

2. stringWithFormat is a class method

Can be directly used nsstring* str = [NSString stringwithformat:@ "%@", @ "Hello World"] call, memory management is autorelease, without manual explicit release

Often in Uilable's fill-in, the use of strings, so it is very easy to cause memory leaks. Here are two ways to compare the correct methods:

There are two solutions:

1.

NSString * str = [[NSString alloc] initwithformat:@ "%@", @ "abc"];

Label.text = str;

[STR release]

Finally in Dealloc [label release]

2.

Label.text = [NSString stringwithformat:@ "%@", @ "abc"];

Also, for function calls, NSString is often used as a return value. So a more correct function is handled as follows:

A method that returns a NSString object that invokes the method in the event. and try the release method to return the NSString object.

[OBJC]View Plaincopy
  1. <span style="color: #454545" >-(nsstring*) createnewstring{
  2. //Case 1-</span><span style= "color: #ff0000" >-need to release </span><span style= "color: #454545" >.
  3. return [[[NSString alloc] initwithformat:@ "%@",@ "1223344"] autorelease];
  4. //Case 2-</span><span style= "color: #ff0000" >-system is automatically released. This method is not recommended because it is unsafe and has ambiguous meanings. </span><span style= "color: #454545" >
  5. return [[[NSString alloc] initwithstring:@ "1223344"] autorelease];
  6. //Case 3-</span><span style= "color: #ff0000" >-system is automatically released. </span><span style= "color: #454545" >
  7. return @ "1223344";
  8. }</span>
  9. Original http://blog.csdn.net/dongdongdongjl/article/details/8471995

iOS Common string NSString Auto-Release comprehension

Related Article

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.