是否有必要在@interface中声明ivars以匹配属性? [英] Is it necessary to declare ivars in @interface to match properties?

查看:108
本文介绍了是否有必要在@interface中声明ivars以匹配属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Objective-C 2.0中的属性和实例变量

我对这两个代码段感到困惑:

I'm confused by these two code segments:

首先:

//.h
@interface Student : NSObject {

}
    @property (nonautomic, copy) NSString *name;
    @property (nonautomic, retain) NSNumber *age;
@end

//.m
@implementation Student
    @synthesize name;
    @synthesize age;
@end

第二名:

//.h
@interface Student : NSObject {
    NSString *name;   // <<============ difference
    NSNumber *age;    // <<============ difference
}
    @property (nonautomic, copy) NSString *name;
    @property (nonautomic, retain) NSNumber *age;
@end

//.m
@implementation Student
    @synthesize name;
    @synthesize age;
@end

这两项都可以。那么是否有必要在 {} 中声明变量?

Both of these can work. So is it necessary to declare variables in the {}?

推荐答案

从现代运行时(x86_64和ARM6 ...和iOS模拟器)开始,您不再需要声明合成的ivars。在第一个例子中,@synthesize正在为你添加实例变量。

Starting with the modern runtime (x86_64 and ARM6...and iOS Simulator) you no longer need to declare synthesized ivars. In the first example @synthesize is adding the instance variable for you.

这篇关于是否有必要在@interface中声明ivars以匹配属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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