iOS - 开始iOS教程 - 下划线变量? [英] iOS - beginning iOS tutorial - underscore before variable?

查看:111
本文介绍了iOS - 开始iOS教程 - 下划线变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习一些iOS开发的时间,我目前有Apress开始IOS6书,我正在工作。

I am starting to learn how some iOS development at the minute and I currently have the Apress beginning IOS6 book that I am working off.

在第二章有一个简单的教程来显示两个按钮和一个标签,当一个按钮被按下时,它显示在被按下的标签上。

In chapter two there is a simple tutorial to show two buttons and a label and when a button is pressed it is shown on the label which one was pressed.

我已经完成了教程,提出一个问题,我找不到答案。

I have completed the tutorial but it has raised one question that I can't find the answer to.

本教程使用ARC(自动引用计数),以防万一有所作为。

The tutorial uses ARC (automatic reference counting) in case that makes a difference.

这是代码,

头文件:

#import <UIKit/UIKit.h>

@interface MTMViewController : UIViewController


@property (weak, nonatomic) IBOutlet UILabel *statusLabel;


- (IBAction)buttonPressed:(UIButton *)sender;

@end

和m文件:

#import "MTMViewController.h"    

@implementation MTMViewController

- (IBAction)buttonPressed:(UIButton *)sender {


    NSString *title = [sender titleForState:UIControlStateNormal];

    NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];

    statusLabel.text = plainText;


}

@end

以上是它出现在书中,但是当在Xcode中教程时,无法编译以下行:

The above is how it appears in the book, however when doing the tutorial in Xcode I could not compile with the following line:

statusLabel.text = plainText;

而是必须将其更改为:

_statusLabel.text = plainText;

当我做这个代码编译并运行正常,我试图找出为什么

When I done this the code compiled and ran fine, I tried to figure out why this happened by going back through the tutorial to see if I missed anything but I didn't see anything.

任何人都可以解释为什么书中的代码没有编译,为什么我没有编译添加下划线到变量的前面?

Can anyone explain why the code in the book didn't compile and why I had to add the underscore to the front of the variable? Is this correct or have I done something wrong?

推荐答案

原因 statusLabel.text = plainText; 失败是因为您没有正确访问该属性。为了通过生成的getters / setter访问它,你需要在它前面加上 self。,因为属性(及其setters / getters)属于self 。因此,它将 self.statusLabel.text = plainText;

The reason statusLabel.text = plainText; failed is because you were not accessing the property correctly. In order to access it through the generated getters/setters, you need to prepend self. to it, as the property (and its setters/getters) belongs to the instance of self. So instead it would be self.statusLabel.text = plainText;

原因 _statusLabel 工作是因为这是保存属性值的基础变量。当以这种方式访问​​变量时,您规避了生成的setters / getter。一般来说,你应该使用 self.propertyName ,因为这将尊重你作为属性定义的一部分提供的关键字(一个很好的例子是,如果你使用 atomic 关键字,因为生成的setters和getter将在底层实例变量周围正确放置 @synchronized 块。

The reason _statusLabel worked is because this is the underlying variable that holds the value for the property. You are circumventing the generated setters/getters when accessing the variable this way. Generally, you should use self.propertyName, as this will respect the keywords you have provided as part of the property definition (a good example is if you use the atomic keyword, as the generated setters and getters will correctly place a @synchronized block around the underlying instance variable).

最近版本的Xcode使用前置下划线创建变量名称,如果您不手动合成您的属性(这是一件好事,以前的人手动合成)。如果您愿意,可以使用 @synthesize statusLabel = m_statusLabel 定义自己的基础变量名称。这意味着您可以使用 m_statusLabel 而不是 _statusLabel 来访问它。你不应该需要这样做通常,除非有急需; Apple建议使用预添加的下划线。

Recent versions of Xcode create the variable name with a prepended underscore if you don't manually synthesize your properties (which is an OK thing to do, previously people had to synthesize manually). You can define your own underlying variable name if you so wish, using @synthesize statusLabel = m_statusLabel. This mean you can access it using m_statusLabel instead of _statusLabel. You shouldn't need to do this usually, unless there is a dire need to; Apple suggests using a prepended underscore.

在初始化方法和取消分配方法时,您应该使用基础变量,因为生成的setters / getter可能不完整在代码中。

You should use the underlying variable when in initialising methods, and deallocating methods, as the generated setters/getters may not be complete when at this point in the code.

这篇关于iOS - 开始iOS教程 - 下划线变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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