在Objective C中是否有两种方法来声明实例变量? [英] Is there 2 methods for declaring instance variables in Objective C?

查看:36
本文介绍了在Objective C中是否有两种方法来声明实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数教程中,声明实例变量的方法是将其放入.h

In most tutorials, the way to declare instance variable is to put it in .h

@interface myViewController: UIViewController {

    UITextField *myTextField;
}

@property (nonatomic, retain) IBOutlet UITextField *myTextField;

和.m

@implementation myViewController
@synthetize myTextField;

但是在斯坦福大学的这一课程中, http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id480479762 这样做的方法是

But in this standford University course http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id480479762 the way to do so is rather

仅在.h中执行:

@interface myViewController: UIViewController
@property (nonatomic, retain) IBOutlet UITextField *myTextField;

在.m中执行以下操作:

In .m do this:

@synthetize myTextField = _myTextField;

它们等效吗?第二种方法是iOS5特有的吗?

Are they equivalent ? Is the second method specific to iOS5 ?

推荐答案

它们在功能上是等效的.在ObjC 2.0中,如果您未在 synthesize 语句中指定一个,则 synthesize 关键字将自动创建关联的ivar.所有现代运行时都具有此功能.

They are functionally equivalent. In ObjC 2.0 the synthesize keyword will automatically create the associated ivar if you do not specify one as part of the synthesize statement. This functionality is present on all modern runtimes.

这篇关于在Objective C中是否有两种方法来声明实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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