What's the purpose of an ivar when a property exists?

前端 未结 3 1904
遥遥无期
遥遥无期 2020-12-03 03:10

The following doesn\'t complain at compilation nor runtime about no name ivar. So why is it so common to see an ivar and @property/@synthesize

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 04:07

    Two not-so-good-but-necessary reasons to make sure properties are backed up by ivars:

    1. For some reason the XCode debugger doesn't show properties that don't have corresponding ivars explicitly declared.
    2. It seems to me that under some circumstances using @property without an ivar can hide other ivars, resulting in compilation errors (see Why does a subclass @property with no corresponding ivar hide superclass ivars? )

    Unless I've got the wrong ends of a couple of sticks here, I think the use of @property without explicit ivars may lead to annoyances not warranted by the convenience.

提交回复
热议问题