调用“@synthesize slider = _slider;”有什么意义?

发布于 2024-12-09 05:17:53 字数 148 浏览 0 评论 0原文

有人可以向我解释创建(似乎是)额外变量的重要性以及为什么在与最后一个同名的新变量之前加下划线。

我读过它与创建实例变量有关,但是为什么要创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亣腦蒛氧 2024-12-16 05:17:54

当您@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 named slider behind the scenes.

The problem here is that you might mistakenly type slider = xxx when you really meant to use self.slider = xxx. When you make something a property, best practice says you should always access it through self.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 use slider instead of self.slider the compiler will give an error message because slider is no longer the name of an instance variable.

您的好友蓝忘机已上羡 2024-12-16 05:17:54

这样做的原因是为了使实例变量从属性点语法中清晰地脱颖而出。它还具有避免参数名称遮蔽实例变量的实际效果,这种情况在某些情况下也会发生。

在大多数情况下,使用实例变量的原因是为了避免在 dealloc 中触发 KVO。如果这样做,您将面临触发 KVO 的风险,您的观察者会收到传递给他们的已释放对象,从而导致 EXC_BAD_ACCESS。

- (void)dealloc
{
    self.slider = nil;
    [super dealloc];
}

因此,通常这样做,这不会触发 KVO,因为您不进行属性访问。

- (void)dealloc
{
    [_slider release];
    [super dealloc];
}

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.

- (void)dealloc
{
    self.slider = nil;
    [super dealloc];
}

So it's common to do this instead, which will not trigger KVO since you don't do property access.

- (void)dealloc
{
    [_slider release];
    [super dealloc];
}
無心 2024-12-16 05:17:54

这通常用于将属性合成为私有前缀或后缀 ivar。它试图防止您意外访问 ivar 而不是属性,或者使用方法参数覆盖 ivar。

考虑一下:

@implementation MYClass
@synthesize flag = flag_;

- (void)doSomethingWithFlag:(BOOL)flag {
  if (flag) {
    // You do not need to worry about confusing the ivar
    // flag and the param flag because it is synthesized to flag_
  }
}

- (void)doSomething {
  if (flag) { // Doesn't work -> use accessor self.flag
    ...
  }
}

@end

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:

@implementation MYClass
@synthesize flag = flag_;

- (void)doSomethingWithFlag:(BOOL)flag {
  if (flag) {
    // You do not need to worry about confusing the ivar
    // flag and the param flag because it is synthesized to flag_
  }
}

- (void)doSomething {
  if (flag) { // Doesn't work -> use accessor self.flag
    ...
  }
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文