Possible Duplicate:
Should I refer to self.property in the init method with ARC?
I’m new to Objective-C and still trying to get my head around everything that’s different (from C# and C). I’m using ARC in my project.
Say I’ve got a constructor like so:
-(id)initWithPriority:(NSNumber *)x1 Value:(id)y1
and I’ve got two (strong) (synthesized) properties (x2,y2). If I do:
_x2=x1;
_y2=y1;
(which skips going through the property and just access the synthesized ivars) rather than
x2=x1;
y2=y1;
does ARC still function (like does it still keep a retain count thingy)?
ARC operates on assignment; otherwise it would be purposeless. (If it had to operate only through properties, it wouldn’t add anything to what existed before ARC.) So yes, if you assign directly to the ivar, the assigned value is retained automatically.
And you should assign directly to the ivar if it is your ivar, because during
init..., your object is not yet completely state-ready. So, ininit..., perform any assignments to your ivars directly to the ivars; do not use the accessors / properties. Example:However, you can assign to your superclass’s properties. So if this were a UIViewController subclass, assigning to
self.titleis fine.For a complete explanation of how ARC works, see my book:
http://www.apeth.com/iOSBook/ch12.html