注意:这个问题似乎很相关,但事实并非如此:关联对象什么时候发布?
我正在为UIView
实例添加第二个描述,如下所示:
- (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);
}
Run Code Online (Sandbox Code Playgroud)
如果是UIView
deallocs,相关描述2是否会被解除分配?有没有办法让这种情况自动发生?
Jod*_*ins 66
如果您想要实际查看整个dealloc时间线的描述,请查看WWDC 2011,Session 322,36:22.但是,这是基本的纲要(我想记住它,所以这是我的代码中的实际注释).
请注意,关联对象在生命周期结束时释放.
// 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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6382 次 |
最近记录: |