现代Objective C运行时中ivar综合的基本机制是什么 [英] What is the underlying mechanism for ivar synthesis in the modern Objective C runtime

查看:83
本文介绍了现代Objective C运行时中ivar综合的基本机制是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现代(64位OS X和iPhone OS)Objective C运行时的功能之一是属性可以动态合成ivars,而无需在类中明确声明它们的功能:

One of the features of the modern (64 bit OS X and iPhone OS) Objective C runtime is the ability for properties to dynamically synthesize ivars without explicitly declaring them in the class:

@interface MyClass : NSObject {
//  NSString *name; unnecessary on modern runtimes
}

@property (retain) NSStrng *name;

@end

@implementation MyClass

@synthesize name;

@end

在我的很多代码中,我使用自定义getter实现来初始化属性:

In quite a bit of my code I use custom getter implementations in order to initialize the properties:

- (NSString *) name {
  if (!name) {
    name = @"Louis";
  }

  return name;
}

以上内容与合成ivars不兼容,因为它需要访问标头中未声明的ivar.由于种种原因,我想更新我的许多个人框架,以便在现代运行时上使用合成的ivars,因此需要修改以上代码以使用合成的ivars,以实现该目标.

The above is incompatible with synthesized ivars since it needs to access a an ivar that is not declared in the header. For various reasons I would like to update a number of my personal frameworks to use synthesized ivars when built on the modern runtimes, the above code needs to be modified to work with synthesized ivars in order to achieve that goal.

尽管Objective C 2.0文档指出,现代运行时上的综合访问器将在首次使用时合成ivar.它没有指定使用哪种低级机制来执行此操作.它是由class_getInstanceVariable()完成的,是否放宽了对class_addIvar()的限制,它是目标C 2.0运行时中未公开的函数吗?虽然我可以为支持我的属性的数据实现自己的边存储,但我宁愿使用综合访问器正在使用的机制.

While the Objective C 2.0 documentation states that the synthesized accessors on the modern runtime will synthesize the ivar on first use. It does not specify what low level mechanism is used to do this. Is it done by class_getInstanceVariable(), are the restrictions on class_addIvar() loosened, is it an undocumented function int he objective C 2.0 runtime? While I could implement my own side storage for the data backing my properties, I would much rather use the mechanism that synthesized accessors are using.

推荐答案

我刚才又去看了一下文档,我认为您在读错它.合成的ivars是在编译时而不是在运行时创建的.

I went and looked at the documentation again just now, and I think you're misreading it. Synthesized ivars are created at compile time, not at run time.

根据 Objective-C 2.0文档 :

取决于运行时的行为存在差异(另请参见运行时差异"):

There are differences in the behavior that depend on the runtime (see also "Runtime Differences"):

对于旧版运行时,必须已在@interface块中声明了实例变量.如果存在与属性具有相同名称和兼容类型的实例变量,则将使用该变量,否则会出现编译器错误.

For the legacy runtimes, instance variables must already be declared in the @interface block. If an instance variable of the same name and compatible type as the property exists, it is used—otherwise, you get a compiler error.

对于现代运行时,实例变量根据需要进行综合.如果已经存在同名的实例变量,则使用它.

For the modern runtimes, instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.

因此,您所需要做的就是声明所需的实例变量,并且相同的代码将在两个运行时都可以工作...

So all you need to do is declare the instance variable you need, and the same code will work on both runtimes...

这篇关于现代Objective C运行时中ivar综合的基本机制是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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