gpt4 book ai didi

objective-c - ARC 是否足够聪明以释放父类(super class)中定义的 subview ?

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:09 24 4
gpt4 key购买 nike

我有一个自定义 View 类,它是 UITableViewCell 的子类。

我还有两个继承自该子类的自定义 View 类(它们共享许多相同的属性,但差异足以保证单独的类)。

我已经在 MyParentCell 中声明了共享属性,还在各自的类中声明了它们的独特属性。

UITableViewCell
|
|
MyParentCell // defines propertyA and propertyB, both IBOutlet subviews
/ \
/ \
/ \
| |
| ChildClass1 // defines propertyC, an IBOutlet subview
|
ChildClass2 // defines property D, an IBOutlet subview

我的问题是:由于我使用的是 ARC,因此无法显式调用 [super delloc];当我定义 dealloc: 时在 ChildClass1ChildClass2 中,我必须释放他们在每个类中拥有的所有 subview ,否则 MyParentCell#dealloc 是否仍会被调用?

即,

我必须这样写吗:

// ChildClass1.m
@implementation ChildClass1

-(void)dealloc
{
self.propertyA = nil;
self.propertyB = nil;
self.propertyC = nil;
}
@end

// ChildClass2.m
@implementation ChildClass2

-(void)dealloc
{
self.propertyA = nil;
self.propertyB = nil;
self.propertyD = nil;
}
@end

或者这样写就够了:

// MyParentCell
@implementation MyParentCell

-(void)dealloc
{
self.propertyA = nil;
self.propertyB = nil;
}
@end

// ChildCell1.m
@implementation ChildCell1

-(void)dealloc
{
self.propertyC = nil;
}
@end

// ChildCell2.m
@implementation ChildCell2

-(void)dealloc
{
self.propertyD = nil;
}
@end

如果第二种方法没问题,有人可以解释何时以及如何调用 MyParentCell#dealloc 吗?

如果第一种方法是必要的,那很糟糕:/

最佳答案

当然,对于 ARC,每个类也只负责清理自己的资源。如果您在子类中定义一个 dealloc,它会在您的方法末尾隐式调用父类的 dealloc。您不必输入它。

如果你只是释放实例变量或属性,你可以依靠 ARC 在整个 dealloc 链完成后为你做这件事。 ARC 静默实现 .cxx_destruct,它从 NSObject 的 dealloc 中调用,并负责释放类中的所有强项。

关于objective-c - ARC 是否足够聪明以释放父类(super class)中定义的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9444561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com