Xcode 4.5 Interface Builder 为 Outlets 添加下划线 [英] Xcode 4.5 Interface Builder Adds Underscores to Outlets

查看:67
本文介绍了Xcode 4.5 Interface Builder 为 Outlets 添加下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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 @property declaration in the header as normal:

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

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

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

viewDidUnload中的代码是正常的:

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

但是,dealloc 中的代码在属性名称前添加了 _:

However, the code in dealloc prepends an _ on the property name:

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

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

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

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

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

推荐答案

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

Yes, the behavior has changed slightly in Xcode 4.5.

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

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

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

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

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

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

当属性被自动合成(没有显式的@synthesize 语句)时,相应的实例变量前面带有下划线.这就是它在您的 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.

这也意味着我无法正常引用该属性

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.

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

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.

这篇关于Xcode 4.5 Interface Builder 为 Outlets 添加下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆