Do not use accessor in the init and DEALLOC functions

Source: Internet
Author: User

do not use accessor in the init and Dealloc functions Article Directory

OBJECTIVE-C 2.0 adds the dot syntax, which is used to simply invoke the accessor of the member variable. Equivalent to Java getter and setter.
Because under normal circumstances, writing a accessor for beginners, it is quite easy to make mistakes. For example, there is a member variable of NSString * called name. One of the wrong spellings is:

-(void) SetName: (NSString *) NewName {
name = NewName;
}

Java classmate certainly can't think of the above code what is wrong? The reason is that objective-c needs to be responsible for the release of memory.
So you need to change the reference before the original object release, the new object, also need to retain, the code is changed to this:

-(void) SetName: (NSString *) NewName {
[Name release];
name = [NewName retain];
}

Beginners may think this is right, in fact, it is still wrong, if NewName and name are pointing to the same object, and this object retain count is only 1.
After the name release, the object is recycled. So it should be changed to:

-(void) SetName: (NSString *) NewName {
if (name! = NewName) {
[Name release];
name = [NewName retain];
}
}

This is a correct set function, Java classmates must be frightened, although know so, but this is more troublesome than Java. As a result, Objective-c allows @property programmers @synthesize to use the + keyword to automatically generate the code (note: Xcode will now automatically add @synthesize keywords by default). So the OBJECTIVE-C programmer was happy. Most of the time there is no need to write getter and setter.

Be careful, however, that the objective-c accessor cannot be used in the INIT and DEALLOC functions! If you write this in Dealloc, there is a problem:

-(void) Dealloc {
Selfnil;
[Super Dealloc]
}

Apple has a dedicated cocoa memory management article in its Developer Documentation Library:
Http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.pdf

The 16th page of the article has a section titled: Don ' t use Accessor Methods in Initializer Methods and Dealloc

The article says: The only place you should not use Accessor is the init function and the Delloc function. In the Init function, for a _count member variable, you should assign a value like this:

-(
Self = [
if (self) {
_count = [[nsnumber alloc] Initwithinteger:
}
Self
}

For an init function with parameters, you should implement the following:

-(ID) Initwithcount: (
Self = [
if (self) {
Copy];
}
Self
}

For the dealloc, the corresponding wording should be the release:

- (

[Super Dealloc];
}

But the more depressed is, the article finally did not say why not! Go to StackOverflow on the search, more unreliable saying is so less once function call, faster. The more plausible argument is that, in Init and dealloc, the presence or absence of an object is uncertain, so sending a message to an object may not be successful.

By the way, when we find this article, we have a lot of such error usage in our code. Although the program does not have a serious memory problem, but in order to insure, or intend to change one line, and then I think, this can be done with vim?
So I went into vim, started the macro recording with QA, and entered


:%s/self./[/g re-enter:%s/= nil/release]/g

Then enter q so that you can use @a to start the macro you just recorded to replace it. Very convenient.

Do not use accessor in the init and DEALLOC functions

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.