加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 属性和实例变量声明

发布时间:2020-12-16 09:30:07 所属栏目:百科 来源:网络整理
导读:我注意到你可以用这种方式“双重声明”一个变量: @interface A { NSString *instanceVariable;}@property (nonatomic,retain) NSString *instanceVariable;@end 这与以下情况具有相同的效果: @interface A {}@property (nonatomic,retain) NSString *insta
我注意到你可以用这种方式“双重声明”一个变量:

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

这与以下情况具有相同的效果:

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

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

解决方法

因为两种方式都有效.

通过声明属性来声明ivar是一个新的语言功能,可以从objc 2.0开始

在“Run-time differences”部分的“Objective-c编程语言”参考文献中指出:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读