调用“@synthesize slider = _slider;”有什么意义?
有人可以向我解释创建(似乎是)额外变量的重要性以及为什么在与最后一个同名的新变量之前加下划线。
我读过它与创建实例变量有关,但是为什么要创建 UI 元素作为实例变量呢?您能提供一个用例吗?
此目标 C 代码用于 iOS 开发。
谢谢。
Can someone explain to me the significance of creating (what seems to be) an extra variable and why put an underscore before a new variable with the same name as the last.
I have read that it is to do with creating instance variables but why would you want to create a UI element as an instance variable? Can you please provide a use case?
This objective c code for use in iOS development.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您
@synthesize
一个属性并且您没有提供自己的getter和setter方法时(在这种情况下不需要@synthesize
),那么总是有一个新实例变量已创建。默认情况下,它的名称与属性相同。因此,@synthesize slider; 在幕后创建了一个名为slider
的实例变量。这里的问题是,当您真正想使用
self.slider = xxx
时,您可能会错误地输入slider = xxx
。当您将某些内容设置为属性时,最佳实践表明您应该始终通过 self.propertyName 访问它(除了 init 和 dealloc 方法以及任何自定义 getter 和 setter 方法)。因此,为了避免这种情况,使用 @synthesize 语句将支持 ivar 重命名为更难以与属性混淆的名称。如果您现在使用
slider
而不是self.slider
,编译器将给出错误消息,因为slider
不再是实例变量的名称。When you
@synthesize
a property and you do not provide your own getter and setter methods (in which case there is no need to@synthesize
) then there is always a new instance variable created. By default it gets the same name as the property. So@synthesize slider;
makes an instance variable namedslider
behind the scenes.The problem here is that you might mistakenly type
slider = xxx
when you really meant to useself.slider = xxx
. When you make something a property, best practice says you should always access it throughself.propertyName
(except in your init and dealloc methods and any custom getter and setter methods).So in order to avoid this, the
@synthesize
statement is used to rename the backing ivar into something that is harder to confuse with the property. If you now useslider
instead ofself.slider
the compiler will give an error message becauseslider
is no longer the name of an instance variable.这样做的原因是为了使实例变量从属性点语法中清晰地脱颖而出。它还具有避免参数名称遮蔽实例变量的实际效果,这种情况在某些情况下也会发生。
在大多数情况下,使用实例变量的原因是为了避免在 dealloc 中触发 KVO。如果这样做,您将面临触发 KVO 的风险,您的观察者会收到传递给他们的已释放对象,从而导致 EXC_BAD_ACCESS。
因此,通常这样做,这不会触发 KVO,因为您不进行属性访问。
The reason for doing that is to make the instance variable clearly stand out from the property dotting syntax. It also has the practical effect of avoiding shadowing of instance variables from argument names, which also occur in some situations.
The reason for using an instance variable at all is in most cases to avoid KVO triggering in dealloc. If you do this, you risk triggering KVO in such a way that your observers gets a deallocated object passed to them, causing an EXC_BAD_ACCESS.
So it's common to do this instead, which will not trigger KVO since you don't do property access.
这通常用于将属性合成为私有前缀或后缀 ivar。它试图防止您意外访问 ivar 而不是属性,或者使用方法参数覆盖 ivar。
考虑一下:
This is commonly used to synthesize the property to a private prefixed or suffixed ivar. It tries to prevent you from accidentally accessing the ivar and not the property or overriding the ivar with a method argument.
Consider this: