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