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

Quick question.

if I have property and ivar declared with the same name:

in the .h file:

(Reminder*)reminder; @property(nonatomic,strong)(Reminder*)reminder; 

in the .m file, should I use ivar or a property in the init method if I use ARC?

 - (id)initWithReminder:(Reminder*)reminder_ { self = [super init]; if (self) { reminder = reminder_; } return self; } 

Or should I use a property to take advantage of automatic reference counting as follows:

 - (id)initWithReminder:(Reminder*)reminder_ { self = [super init]; if (self) { self.reminder = reminder_; } return self; } 

I am not sure at what point in the initialization of the object properties become available using dot notation.

+48
initialization objective-c automatic-ref-counting ios5 reference-counting
Nov 08 2018-11-11T00:
source share
3 answers

Use direct access in partially constructed states independent of ARC:

 - (id)initWithReminder:(Reminder*)reminder_ { self = [super init]; if (self) { reminder = reminder_; // OR reminder = [reminder_ retain]; } return self; } 

This is because self.whatever will cause other side effects, such as key-value notifications (KVO), or maybe your class implements (explicitly) or overrides the setWhatever: subclass setWhatever: - and this may lead to partial initialization an instance for other APIs (including its own) that rightly assume that they are dealing with a fully constructed object.

You can manually verify that the class is able to work in a partially initialized state, but this requires a lot of maintenance and (in truth) is impractical or impossible when other people want to subclass your class. This requires a lot of time and maintenance, and it does not have significant benefits, especially if you try to use this approach as a convention.

Thus, a uniform method that guarantees correctness is to use direct access in partially constructed states and to avoid the use of accessories.

Note. I use "partially built" because initialization is only half the image; -dealloc has similar reservations.

More information on why you should use direct access in partially constructed states (ARC || MRC) can be found here: Property initialization, dot notation

+66
Nov 08 '11 at 19:50
source share

No, not worth it!

You can find a description of why here
Also, apples recommend not doing this. Read here

+4
Mar 20 '14 at 8:49
source share

I am not sure at what point in the initialization of the object properties become available using dot notation.

Since the dot notation is still an Objective-C method (and the C method is actually the ObjC method), dot notation or method invocation is completely safe. The GIVEN method is ready to work with the base type (s) in memory, no matter what state they are in. The normal rule to avoid using an uninitialized (possibly) garage memory segment will still apply. Which is the strongest motivation for using ivar in init.

But if your method (getter | setter) is able to correctly use the memory segment - regardless of whether it is written before it is read - then, in any case, use your getter in the init method. Lazy getter uses the assumption that the pointer it initializes is started as "nil" to decide whether to initialize. If you cannot read the original contents of your memory, then initializing ivar may be the safest course.

Why is there a rule to never use setters or getters in init if the method is able to work correctly in this scenario?

0
Oct 30 '15 at 21:04
source share



All Articles