Difference between class property mVar and instance variable self.mVar

前端 未结 3 576

I am some what confused as to the difference between accessing an instance variable via self or just by name (when working inside the class).

For instance, take this

3条回答
  •  自闭症患者
    2020-12-10 20:48

    First of all, with a read-only property--which an IBOutlet essentially is--it does not matter as much.

    The key difference is that the property is actually calling the accessor method while the instance variable is being accessed directly.

    Thus, for setting a retain property, using self and the accessor will release the old object and retain the new one. Setting the instance variable directly will NOT impact the retain counts of any objects.

    Using @synthesize will generate standard accessors for you.

    The key reason to use properties is that, since they are accessors, they can be read and/or modified from outside the class.

提交回复
热议问题