是否会自动发布相关对象? [英] Will An Associated Object Be Released Automatically?

查看:118
本文介绍了是否会自动发布相关对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这个其他问题似乎相关但不是:关联对象什么时候发布?

Note: This other question seems relevant but it's not: When does an associated object get released?

我正在向 UIView 实例添加第二个描述,如下所示:

I'm adding a second description to a UIView instance as follows:

- (void) setSecondDescription:(UIView*)view description2:(NSString*)description2 {
    objc_setAssociatedObject (view,&key,description2,OBJC_ASSOCIATION_RETAIN);
}

- (NSString*) secondDescription:(UIView*)view {
    return (id)objc_getAssociatedObject(view, &key);   
}

如果 UIView deallocs,相关描述2是否会被解除分配?有没有办法让这种情况自动发生?

If the UIView deallocs, will the associated description 2 get dealloced? Is there any way to get this to happen automatically?

推荐答案

如果你想实际有关整个dealloc时间表的描述,请参阅WWDC 2011,Session 322,36:22。但是,这是基本的纲要(我想记住它,所以这是我的代码中的实际注释)。

If you want to actually see the description of the entire dealloc timeline, look at WWDC 2011, Session 322, 36:22. However, here's the basic rundown (I wanted to remember it, so this is an actual comment in a piece of my code).

注意,关联的对象被释放在生命周期结束时。

Note, that the associated objects are released at the end of the life cycle.

// General Information
// We take advantage of the documented Deallocation Timeline (WWDC 2011, Session 322, 36:22).
// 1. -release to zero
//     * Object is now deallocating and will die.
//     * New __weak references are not allowed, and will get nil.
//     * [self dealloc] is called
// 2. Subclass -dealloc
//     * bottom-most subclass -dealloc is called
//     * Non-ARC code manually releases iVars
//     * Walk the super-class chain calling -dealloc
// 3. NSObject -dealloc
//     * Simply calls the ObjC runtime object_dispose()
// 4. object_dispose()
//     * Call destructors for C++ iVars
//     * Call -release for ARC iVars
//     * Erase associated references
//     * Erase __weak references
//     * Call free()

这篇关于是否会自动发布相关对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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