IPhone/Mac Objective-C memory management tutorial and Principle Analysis (3) @ property (retain) and @ synthesize default implementation

Source: Internet
Author: User

Copyright Notice

This article is copyrighted by Vince Yuan (vince. yuan # gmail.com. Welcome to non-profit reprint, reprint must contain the original link http://vinceyuan.cnblogs.com, and must contain the complete content of this copyright statement.

 

Version 1.1 was published on

 

3. Default Implementation of @ property (retain) and @ synthesize

Here we will explain what happened to @ property (retain) ClassB * objB; and @ synthesize objB; (default Implementation of retain property ). Property is actually getter and setter, for the property with the retain parameter, the implementation behind it is as follows (see the memman-getter-setter.m in the attachment, you will find that the result is the same as the memman-property.m ):

@ Interface ClassA: NSObject

{

ClassB * objB;

}

 

-(ClassB *) getObjB;

-(Void) setObjB :( ClassB *) value;

@ End

 

@ Implementation ClassA

-(ClassB *) getObjB

{

Return objB;

}

 

-(Void) setObjB :( ClassB *) value

{

If (objB! = Value)

{

[ObjB release];

ObjB = [value retain];

}

}

In setObjB, if the new value is different from the original value, you must release the original value object once to ensure that retain count is correct.

Because we retain the class once (though implemented by default), we need to release the member variable in the dealloc method.

-(Void) dealloc

{

[ObjB release];

[Super dealloc];

}

 

 

Sample Code File Link: http://files.cnblogs.com/VinceYuan/objective-c-memman.zip

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.