Should IBOutlets be ivars or properties?

前端 未结 2 2035
情书的邮戳
情书的邮戳 2021-02-06 09:24

Though I\'m sure they exists, I\'m having difficulties finding or pinning down an official best practice for declaring outlets in a ViewController.

There are 3 options s

2条回答
  •  爱一瞬间的悲伤
    2021-02-06 09:43

    Here's my understanding

    Use properties for variables that will be accessed by other classes, either read from (getters) or written to (setters). Both setters and getters are synthesized for properties.

    Use ivars for variables that will be used internally by the owning class only, that is, other classes will not set or get their values.

    Sure you can use properties in lieu of ivars, but they incur the function-call overhead whenever they're accessed. So if you have an internal variable that is accessed by your class a LOT, the function calls will affect the real-time performance, and this can be avoided by declaring them as ivars.

提交回复
热议问题