什么是现代运行时? [英] What is modern runtime?

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

问题描述

注意:通常在 dealloc 方法中,您应该直接释放对象实例变量(而不是调用set访问器并传递nilas参数) ,如本例所示:

Note: Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nilas the parameter), as illustrated in this example:

- (void)dealloc {
    [property release];
    [super dealloc];
}

如果您正在使用现代运行时并合成实例变量,那么,无法直接访问实例变量,因此您必须调用访问器方法:

If you are using the modern runtime and synthesizing the instance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:

- (void)dealloc {
    [self setProperty:nil];
    [super dealloc];
}

iOS应用程序开发中的现代运行时到底是什么?

What is modern runtime in iOS application development exactly?

推荐答案

可以使用与合成属性相同的名称直接访问ivar。 @synthesize 指令代表您创建ivar(如果尚不存在),并且因为它是编译器指令,所以ivar在编译时可用。参见运行时差异。正如Abizern在评论中指出的那样,也可以为ivar指定你喜欢的名字: @synthesize coffee = tea; - 这里,是ivar和咖啡该属性。

It is possible to access the ivar directly, under the same name as the synthesized property. The @synthesize directive creates the ivar on your behalf if one does not already exist, and since that is a compiler directive, the ivar is available at compile-time. See "Runtime Difference" in the Declared Properties chapter of The Objective-C Programming Language. As Abizern noted in a comment, it's also possible to specify whatever name you like for the ivar: @synthesize coffee=tea; -- here, tea is the ivar and coffee the property.

要使用ivar,只需像任何其他变量一样引用它,而不使用点语法。以下内容完全合法且符合预期:

To use the ivar, simply refer to it like any other variable, without using the dot syntax. The following is all perfectly legal and works as expected:

@interface Grisby : NSObject {}
@property (retain) NSObject * obj;
@end

@implementation Grisby

@synthesize obj;

- (void) dealloc {
    [obj release], obj = nil;
    [super dealloc];
}

- (id) init {
    self = [super init];
    if( !self ) return nil;

    obj = [NSObject new];

    return self;
}

- (NSObject *) obj {
    return [[obj retain] autorelease];
}

@end

现代运行时是与Mac OS X 10.5(Leopard)一起推出,作为向64位过渡的一部分。所有版本的iOS都使用现代运行时。合成的实例变量是现代运行时的一个特性,如我在上面提供的链接中所述。

The "modern runtime" was introduced with Mac OS X 10.5 (Leopard) as part of the transition to 64-bit. All versions of iOS use the modern runtime. Synthesized instance variables are a feature of the modern runtime, as noted in the link I provided above.

另一个关键区别,在运行时中注明Objective-C运行时编程指南的版本和平台是实例变量非易碎。在ivar存储和访问中添加了一层间接,允许类添加变量而不影响派生类的存储。它也可能促进实例变量合成。 Greg Parker 有一个涉及小猫的解释,在迈克阿什的 2009运行时写入,这里的Bavarious有一个 swell post 关于ivar存储和类扩展。

The other key difference, noted in "Runtime Versions and Platforms" of the Objective-C Runtime Programming Guide, is that instance variables are "non-fragile". There is a layer of indirection added to ivar storage and access which allows classes to add variables without affecting the storage of derived classes. It also presumably facilitates instance variable synthesis. Greg Parker has an explanation involving kittens, there's passing reference to it in Mike Ash's 2009 runtime writeup, and Bavarious here on SO has a swell post about ivar storage and class extensions.

你可以看到其他改变的东西,但没有解释,在Mac OS X版本10.5 Delta Objective-C运行时参考的章节。

You can see other things that changed, though without explanation, in the "Mac OS X Version 10.5 Delta" chapter of the Objective-C Runtime Reference.

这篇关于什么是现代运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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