I saw a sample code with this code:
in the .h file:
CALayer *_animationLayer;
@property (nonatomic, retain) CALayer *animationLayer;
And in the .m file:
@synthesize animationLayer = _animationLayer;
I am guessing it is related to the retain count?
Can someone explain that?
The code in the .h file is declaring two things, a variable called
_animationLayerwhich is of typeCALayer*, and a property calledanimationLayerwhich is also of typeCALayer*. The code in the .m file is instructing the objective-c compiler to automatically generate getters and setters for theanimationLayerproperty, using the_animationLayervariable to hold the actual value that is set.So for instance:
And you’re correct, this does have some relationship to the object’s retainCount (so it’s not quite correct to think of the variable as a direct alias for the property). In essence, setting the value directly using the
_animationLayervariable does not retain the new value or release the old value. Setting the value using the property accessor does. For example: