gpt4 book ai didi

objective-c - Objective C : Why do we declare ivars in the . h 成员(member)区 if @property 好像是自动做的?

转载 作者:太空狗 更新时间:2023-10-30 03:18:08 26 4
gpt4 key购买 nike

在实现接口(interface)时,教程和文献中的常见方法似乎是声明一个 ivar,然后设置 @property,然后设置 @synthesize

@interface MyClass : NSObject {
NSString *myString;
}

@property (nonatomic, retain) NSString *myString;
@end

但是,省略显式声明并仅放置@property 具有相同的效果。

@interface MyClass: NSObject {
}

@property (nonatomic, retain) NSString *myString;
@end

那么为什么大多数人会使用 @property 和显式声明呢?不这样做是不好的形式吗?

最佳答案

它曾经是必要的。有两个不同版本的 Objective-C 运行时:仅 32 位的“传统”运行时(旧版本)和 32/64 位运行时(新的 32 位运行时仅用于 iOS 设备和iOS 模拟器)。

我认为唯一仍然需要的地方是当您在 32 位模式(10.5 或 10.6)下运行应用程序时。其他任何地方(64 位 Leopard、64 位雪豹、Lion、iOS)都使用具有“自动 ivar 合成”的较新运行时,生成的 ivar 称为“合成 ivar”。

关于objective-c - Objective C : Why do we declare ivars in the . h 成员(member)区 if @property 好像是自动做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5555736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com