Use of class clusters in OC and the use of attributes in initialization methods

Source: Internet
Author: User

Today we have a few strings related memory issues, and share with you

NSString *name = [[NSString alloc]initwithstring:@ "Zhang San"];
NSLog (@ "%d", [name Retaincount]);

These two lines of code print the result is-1,

NSString * astring = [[NSString alloc] initwithformat:@ "123"];
NSLog (@ "%d", astring.retaincount);

These two lines of code also print the result-1

NSString * astring = [[NSString alloc] initwithformat:@ "1233sfsf4545f"];
NSLog (@ "%d", astring.retaincount);

When the string is changed to the above, the print result becomes 1

It's strange why the same syntax will result in a different print . In turn, the print result is a 1 description of the string that refers to the constant area, and the print result is a string of 1 points to the heap area. Since OC is not open source, the internal implementation is not known, but we can at least guess that OC in different sizes of strings are different methods.

Initwithstring produces a string that points to a constant area, which cannot be set, and if destroyed with Dealloc, it will be an error. One: You cannot call the Dealloc method manually and it is clear from Apple's official documentation that the objects created and the retain objects are kept by themselves, all of which are in the heap area. The memory of the static zone is actually compiled and allocated, its memory address is very forward, and the entire stage of the program is running, so we can not release.

About class cluster The most cited example is the NSNumber class, in fact NSNumber class is an abstract superclass, there are a lot of specific subclasses, such as Nsint nsdouble, they correspond to different initialization methods, That is, the different initialization methods of NSNumber return different types. Not only is nsnumber,nsstring also so,

//Use of class clusters
IdSomeClass = [NSStringAlloc];//Type of object returned:Nsplaceholderstring

NSString*string1 = [[NSStringAlloc]Init];//Type of object returned:__nscfconstantstring
NSString*string2 = [[NSStringAlloc]Initwithformat:@ "string2"];//Type of object returned:__nscfstring
  nslog@ "%@"   nslog< Span style= "color: #555555; Font-family:menlo; font-size:13px; line-height:24px; " > (@ "%@" , string2);

Class clusters can simplify an object-oriented public schema without diminishing the richness of functionality

In the project we will certainly encounter the class initialization method of the case, if the actual MRC mode, how to ensure that the memory is not leak, Apple's official document made the following recommendations

-(ID) Initwithname: (nsstring*) name{

self = [super init];

if (self) {

_name = [name copy];//of course the semantics of the name attribute control to use the copy

}

return self;

}

Use self.name = name; Actually, it's the same as above.

This article is from the "love life does Not Love Bug" blog, please be sure to keep this source http://10279592.blog.51cto.com/10269592/1656503

Use of class clusters in OC and use of properties in initialization methods

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.