With ARC why use @properties anymore?

前端 未结 5 1522
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 10:25

In non-ARC code retained properties handily take care of memory management for you using the self.property = syntax, so we were taught to use them for practical

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 11:16

    so does the reason for using properties evaporate?

    With ARC making the "ownership magic" available to ivars, this particular aspect of why one would choose properties over ivars does evaporate. However, many others remain:

    • atomic/nonatomic distinction is available for properties, not for ivars
    • flexibility afforded by properties (readonly/read+write distinction) is not available for ivars
    • ability to perform calculation and argument checking is not available to ivars

    I continue using properties as my default way of keeping state that may be exposed to outside classes or internal "sibling" classes, because additional flexibility more than outweighs the small additional cost at run-time.

提交回复
热议问题