0

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)?

Community
  • 1
  • 1
Mr. Smith
  • 4,288
  • 7
  • 40
  • 82
  • 1
    By the way, [Don’t Use Accessor Methods in Initializer Methods and `dealloc`](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6). Otherwise, [Use Accessor Methods to Set Property Values](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW5). – Rob Nov 09 '12 at 23:37
  • Also, I'm assuming that in that example, `x2=x1;`, you meant to say `self.x2 = x1;` or `[self setX2:x1];`. – Rob Nov 09 '12 at 23:43

3 Answers3

4

You would set the instance variable directly, and not use the accessor. ARC will perform the reference counting.

Note: You will also want to preserve the semantics of your property in your initializer's implementation. For example, if it declared copy, you would then assign a copy of the parameter to the ivar in the initializer.


Additional Reading:

Should I refer to self.property in the init method with ARC?

Initializing a property, dot notation

Community
  • 1
  • 1
justin
  • 104,054
  • 14
  • 179
  • 226
3

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, in init..., perform any assignments to your ivars directly to the ivars; do not use the accessors / properties. Example:

- (id) initWithName: (NSString*) s { 
    self = [super init]; 
    if (self) {
        self->_name = [s copy]; //
    }
    return self; }

However, you can assign to your superclass's properties. So if this were a UIViewController subclass, assigning to self.title is fine.

For a complete explanation of how ARC works, see my book:

http://www.apeth.com/iOSBook/ch12.html

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • @justin It _is_ fine, as proven by the fact that Apple does it all the time, e.g. here (just the first one I stumbled across at random): https://developer.apple.com/library/ios/#samplecode/CollectionView-Simple/Listings/CollectionView_Cell_m.html#//apple_ref/doc/uid/DTS40012860-CollectionView_Cell_m-DontLinkElementID_6 The reason is that we've already called super, so the aspect of ourselves that comes from the superclass is ready. Plus in many cases there is just no better place than the initializer to set certain superclass properties, and the property / accessor is your only way in. – matt Nov 10 '12 at 00:21
  • 1
    no... it's not fine :) consider everything the superclass may call as side-effects from the implementation of the setter. referring to the sample/example you linked: say setting self's background view adds the view to self's view graph -- well, you and your non-constructed subclass implementations would receive resize and layout commands -- all before they have had a chance to initialize. same problem and side effects seen here: http://stackoverflow.com/questions/5932677/initializing-a-property-dot-notation/5932733#5932733 (cont) – justin Nov 10 '12 at 00:35
  • (cont) only in some cases can you prove the exact side effects of calling a dynamic method, but that is a rather heavy, manual maintenance burden so it's just a bad idea IMO. generally safe? no. i don't care if it comes from apple. there's much room for improvement in their samples and the programs they provide (fortunately, the quality has generally improved over the years IMO). if you pay a very good developer to review their sample code, they will tell you. there have been blogs about it too. (cont) – justin Nov 10 '12 at 00:37
  • (cont) their best and brightest don't always write the sample code, or the writer may make 'shortcuts' to focus on the point the sample is designed to illustrate (often made in comments). – justin Nov 10 '12 at 00:40
3

Yes, you can assign instance variables directly.

Without ARC, you would need to retain the object:

[_x1 release];
_x1 = [x1 retain];

With ARC, you can just assign the instance variable:

_x1 = x1;
Darren
  • 25,520
  • 5
  • 61
  • 71