Xcode 4.5 Interface Builder 为 Outlets 添加下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12549909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:35:11  来源:igfitidea点击:

Xcode 4.5 Interface Builder Adds Underscores to Outlets

objective-cxcodepropertiesoutlet

提问by eattrig

Does Xcode 4.5 have a slightly different manner of auto-generating properties and their associated memory release sections (in dealloc and viewDidUnload)?

Xcode 4.5 自动生成属性及其相关内存释放部分的方式是否略有不同(在 dealloc 和 viewDidUnload 中)?

I upgraded to Xcode 4.5 from 4.5 Beta 1 yesterday. Now when I use Interface Builder to create an outlet (by Ctrl-dragging from, say, a UILabel to the associated header file), it creates the @propertydeclaration in the header as normal:

我昨天从 4.5 Beta 1 升级到 Xcode 4.5。现在,当我使用 Interface Builder 创建一个插座时(例如,通过 Ctrl 从 UILabel 拖动到关联的头文件),它会@property像往常一样在头文件中创建声明:

@property (retain, nonatomic) IBOutlet UILabel *propertyName;

However, in the associated .m file, there is no @synthesizedeclaration.

但是,在关联的 .m 文件中,没有@synthesize声明。

The code in viewDidUnloadis normal:

中的代码viewDidUnload是正常的:

- (void)viewDidUnload {
    [self setPropertyName:nil];
    [super viewDidUnload];
}

However, the code in deallocprepends an _on the property name:

但是,中的代码在属性名称dealloc前面加上了一个_

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

This also means I cannot reference the property as normal ([propertyName doSomething];)

这也意味着我不能像往常一样引用该属性 ( [propertyName doSomething];)

Did something change? Or did I accidentally coincidentally change some setting?

有什么改变吗?还是我不小心更改了某些设置?

回答by Darren

Yes, the behavior has changed slightly in Xcode 4.5.

是的,Xcode 4.5 中的行为略有变化。

... in the associated .m file, there is no @synthesize declaration.

...在关联的 .m 文件中,没有 @synthesize 声明。

In Xcode 4.5, the @synthesizestatement is now optional, and properties are synthesized automatically. Automatically generated IBOutlet properties therefore no longer add @synthesizebecause it's no longer required.

在 Xcode 4.5 中,该@synthesize语句现在是可选的,并且属性会自动合成。@synthesize由于不再需要自动生成的 IBOutlet 属性,因此不再添加。

... the code in dealloc prepends an _ on the property name

... dealloc 中的代码在属性名称前添加一个 _

When properties are synthesized automatically (without an explicit @synthesize statement) the corresponding instance variable is prepended with an underscore. That's why it shows up as such in your dealloc method. This is so the instance variable and property names don't overlap.

当属性被自动合成(没有显式的@synthesize 语句)时,相应的实例变量前面带有下划线。这就是它在您的 dealloc 方法中显示的原因。这是为了实例变量和属性名称不重叠。

This also means I cannot reference the property as normal

这也意味着我不能正常引用该属性

No. Accessing instance variables and properties has not changed. All that has changed is the default name of the instance variable. E.g.:

不会。访问实例变量和属性没有改变。所有改变的是实例变量的默认名称。例如:

_foo = @"Bar"; // Setting an instance variable directly.
self.foo = @"Bar";  // Setting an instance variable via a property accessor method.

The underscore is merely a matter of style, so that it's more clear that you're accessing the instance variable rather than the property.

下划线只是样式问题,因此更清楚的是您正在访问实例变量而不是属性。

Note that you can add a @synthesize statement yourself, and that will force the name of the corresponding instance variable to be whatever you want it to be. Likewise, if you add your own property accessor methods then that will prevent the instance variable from being generated automatically.

请注意,您可以自己添加 @synthesize 语句,这将强制相应实例变量的名称成为您想要的任何名称。同样,如果您添加自己的属性访问器方法,则会阻止自动生成实例变量。

回答by Carl Veazey

Xcode now auto-synthesizes all properties to be an instance variable called _propertyName. In many cases you no longer need @synthesizeor to declare an instance variable explicitly.

Xcode 现在自动将所有属性合成为一个名为_propertyName. 在许多情况下,您不再需要@synthesize或显式声明实例变量。

回答by aLevelOfIndirection

With XCode 4.5 the default is now to add the underscore to the underlying instance variable. But the only impact this has for you is that you need to refer to that variable with the underscore. Here is an example:

使用 XCode 4.5,现在默认是将下划线添加到基础实例变量中。但这对您的唯一影响是您需要使用下划线引用该变量。下面是一个例子:

@property (retain) UIColor color;

In your implementation code you can refer to the color like this:

在您的实现代码中,您可以像这样引用颜色:

[_color set]; // Now do some drawing...

For the outside world things remain unchanged.

对于外面的世界,事情没有改变。

object.color = [UIColor redColor];

This change is based on a common practice to name instance variables with an underscore prefix to distinguish them from local variables in your code.

此更改基于使用下划线前缀命名实例变量的常见做法,以将它们与代码中的局部变量区分开来。

回答by Vervious

Also, note that you normally want to access your property as [self.propertyName doSomething];, which is why auto-prefixing the instance variable with an underscore to indicate privacy makes sense.

另外,请注意,您通常希望将您的属性访问为[self.propertyName doSomething];,这就是为什么使用下划线自动为实例变量添加前缀以指示隐私是有意义的。

If you really have to, you access it as [_propertyName doSomething]if you let it auto synthesize, but that bypasses the getter. Or, you can @synthesize propertyName = propertyName;to be able to use [propertyName doSomething]again, but why would you?

如果你真的需要,你可以像[_propertyName doSomething]让它自动合成一样访问它,但这会绕过 getter。或者,您可以再次@synthesize propertyName = propertyName;使用[propertyName doSomething],但您为什么要这样做?

回答by Josh G

I have found that if you use an underscore before your @property name you are again able to implement your desired methods like before.

我发现如果您在@property 名称前使用下划线,您就可以再次像以前一样实现您想要的方法。