目标C:如果@property似乎自动执行,为什么要在.h成员区域中声明ivars? [英] Objective C: Why do we declare ivars in the .h member area if @property seems to do it automatically?

查看:84
本文介绍了目标C:如果@property似乎自动执行,为什么要在.h成员区域中声明ivars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现界面时,教程和文献中常见的方法是声明一个ivar,然后设置 @property 然后 @synthesize

In implementing an interface it seems the common method in tutorials and literature is to declare an ivar and then set the @property then @synthesize.

@interface MyClass : NSObject {
NSString *myString;
}

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

但是,省略明确的声明,只是放置@property具有相同的效果。

However, omitting the explicit declaration and just putting @property has the same effect.

@interface MyClass: NSObject {
}

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

所以大多数人如何使用 @property 和明确的声明?不好吗?

So how come most people use @property and an explicit declaration? Is it bad form not to?

推荐答案

以前是必要的。 Objective-C运行时有两个不同版本:仅32位的旧运行时(旧版)和32位/ 64位运行时(新的32位运行时仅在iOS设备上使用, iOS模拟器)。

It used to be necessary. There are two different versions of the Objective-C runtime: a 32-bit-only "legacy" runtime (the old one) and a 32/64-bit runtime (the new 32-bit runtime is only used on iOS devices and for the iOS simulator).

我认为唯一需要的地方是当您以32位模式(10.5或10.6)运行应用程序时。其他地方(64位Leopard,64位Snow Leopard,Lion,iOS)使用了具有自动ivar综合的较新的运行时,并将所产生的ivars称为合成ivars。

I think the only place this is still necessary is when you're running the app in 32-bit mode (either 10.5 or 10.6). Everywhere else (64-bit Leopard, 64-bit Snow Leopard, Lion, iOS) uses the newer runtime that has "automatic ivar synthesis", and the resulting ivars are referred to as "synthesized ivars".

这篇关于目标C:如果@property似乎自动执行,为什么要在.h成员区域中声明ivars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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