[super ... any method name]的最佳用法是什么?最近,我发现在dealloc中,[super dealloc]必须位于同一端。因为如果我们在[super dealloc]之后设置它,那么以前未使用的任何变量都可以用垃圾填充。这是罕见的事情,但是有可能。之后,我们的应用程序将崩溃。

因此, super 方法的最佳用法是什么,例如-(void)viewWillAppear:(BOOL)animated的最佳用法。正文开头或结尾处[super viewWillAppear:(BOOL)animated]的最佳位置在哪里?

最佳答案

通常的经验法则是,当您覆盖执行某种初始化的方法时,请先调用super,然后再执行操作。当您重写某种拆卸方法时,您将调用super last:

- (void) setupSomething {
    [super setupSomething];
    …
}

- (void) tearDownSomething {
    …
    [super tearDownSomething];
}

第一种方法是init…viewWillAppearviewDidLoadsetUp。第二类是deallocviewDidUnloadviewWillDisappeartearDown。这不是硬性规定,它只是从方法的工作中得出的。

09-06 10:31
查看更多