Is self.iVar necessary for strong properties with ARC?

喜你入骨 提交于 2019-11-29 03:17:47

From a memory management perspective, using ivar = ... or self.property = ... (note: there's no such thing as self.ivar) are the same. However, using ivar = ... doesn't invoke the setter while self.property = ... does. This has 3 important ramifications, in no particular order:

  1. If the property is not marked nonatomic, then access to the underlying ivar will not take the lock and you will be breaking the atomicity implications.
  2. If the property is overridden, either by you or by a subclass, the overridden setter will not be invoked.
  3. KVO notifications will not be sent.

As for only declaring the ivar, it has the same memory management semantics as declaring a local variable. This is documented in section 4.4 of the Objective-C Automatic Reference Counting document, but basically, if it's an object, it will be inferred to be __strong.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!