属性和实例变量声明 [英] Properties and instance variable declaration

查看:94
本文介绍了属性和实例变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到您可以通过以下方式双重声明"变量:

I notice that you can 'double declare' a variable in this way:

@interface A {
    NSString *instanceVariable;
}
@property (nonatomic, retain) NSString *instanceVariable;
@end

这与刚才的效果相同:

@interface A {
}
@property (nonatomic, retain) NSString *instanceVariable;
@end

为什么在这种情况下编译器不会抱怨?

Why doesn't the compiler complain in situations like this?

推荐答案

因为两种方法都有效.

仅通过声明属性来声明ivar是从objc 2.0开始可用的新语言功能

Declaring ivar via just declaring a property for it is a new language feature available starting objc 2.0

部分:

让@synthesize在旧版中工作 运行时,您必须提供一个 具有相同名称的实例变量 属性的兼容类型或 指定另一个现有实例 @synthesize语句中的变量. 使用现代运行时,如果您不这样做 提供一个实例变量, 编译器会为您添加一个.

For @synthesize to work in the legacy runtime, you must either provide an instance variable with the same name and compatible type of the property or specify another existing instance variable in the @synthesize statement. With the modern runtime, if you do not provide an instance variable, the compiler adds one for you.

这篇关于属性和实例变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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