difference between _var and self.var

。_饼干妹妹 提交于 2019-12-06 03:46:24

_var is just a different name for the instance variable (presumably so you don't accidentally access directly it when you meant to use an accessor). It doesn't have any special meaning in the language beyond just being a valid ivar name.

When you define a @property like: @property (nonatomic, strong) NSString *var;, Objective-C 2.0 and above automatically, as of 2012, @synthesizes that property to create three things:

  • An underlying instance variable of NSString *_var.
  • A method for getting the underlying instance variable: -(NSString *)var {}
  • A method for setting the underlying instance variable: -(void)setVar:(NSString *)newVar {}

Generally, it is not good practice to directly access or set the underlying instance variable directly due to messing with KVO and bypassing side effects that might have been placed into either the getter or setter methods.

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