1

As far as I know you should always use accessors to access or modify a property, except in two scenarios:

  • init
  • dealloc

Don’t Use Accessor Methods in Initializer Methods and dealloc The only places you shouldn’t use accessor methods to set an instance variable are in initializer methods and dealloc. To initialize a counter object with a number object representing zero, you might implement an init.

This exceptions are because calling accessors when the view is not completely initialised might raise issues when overriding the setters/getters (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html)

So, according to this the use of accessors on viewDidLoad should be perfectly fine, and even recommended, however in most of the codes available on internet developers use _ivars on viewDidLoad and I wonder why. Is there a reason for using property _ivars instead of accessors in viewDidLoad?

Pablo A.
  • 2,042
  • 1
  • 17
  • 27

3 Answers3

0

One of the most valuable post about using accessors in init/dealloc method https://www.mikeash.com/pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html

Proton
  • 1,335
  • 1
  • 10
  • 16
  • The post just explains what I already said about using accessors on init and dealloc, it doesn't answers my question. – Pablo A. Jun 29 '16 at 11:29
  • Some benefits if you use property: you can put breakpoint on the getter/setter method, override getter/setter method. – Proton Jun 29 '16 at 11:35
  • And the benefit of `ivars`: You can use it as @public, @protected, @private easier than property – Proton Jun 29 '16 at 11:38
  • I meant `_ivars` from properties, and the benefits of `ivars` doesn't answer why to use them only on `viewDidLoad`. My question is why to use property `_ivars` from viewDidLoad instead of accessors as in any other method (except `init` and `dealloc`) – Pablo A. Jun 29 '16 at 11:45
  • I also found many code in github use property in `viewDidLoad`. I think it's up to developer. – Proton Jun 29 '16 at 11:47
0

you should always use accessors to access or modify a property

Where did you read this?

When you want to access a property, you can use _ivar (or self->_ivar) to access the instance variable directly without passing by the getter, it would be faster that resolving and executing the getter. However, if you use a custom getter then it won't be executed.

hyde
  • 36
  • 4
  • Apple documentation especifies `Use Accessor Methods to Set Property Values` – Pablo A. Jun 29 '16 at 11:52
  • Yep, but it is not mandatory, you can use both, but accessor methods are easier for memory management. For instance if you set a property with the `copy` attribut, setting directly the ivar won't make a copy, because it is the role of the setter. – hyde Jun 29 '16 at 12:25
  • Plus, before ARC with a `retain` property, you would need to release/retain your ivar before/after setting it. But with ARC this is no more an issue, setting a strong ivar will release/retain it automaticaly – hyde Jun 29 '16 at 12:26
0

Often property accessors are overridden to update the view, which is inefficient if the view is not visible yet. So it is possible the code you found that ivar directly is using that to prevent that. However, much better is to use the property but check if the view is visible and only update if necessary. The same method can be called from viewWillAppear.

malhal
  • 26,330
  • 7
  • 115
  • 133