A new iOS ViewControllers created from a template contains several "boilerplate" methods that call their parent class methods.
从模板创建的新iOS ViewControllers包含几个调用其父类方法的“样板”方法。
-(void) viewDidLoad {
[super viewDidLoad];
}
- (void) viewDidUnload {
[super viewDidUnload];
}
- (void) dealloc {
[super dealloc];
}
When modify these classes, should I put my own code before or after the parent class calls?
修改这些类时,我应该在父类调用之前或之后放置我自己的代码吗?
- (void) viewDidLoad {
// Should I put my code here?
[super viewDidLoad];
// Or here?
}
2 个解决方案
#1
3
This is applicable for all OOP in general. In constructor (and in other methods too) you should call the parent's constructor before your code. The reason is your code may require some initialization which are handled in parent, i.e. initialization of base should go before initialization of derived class. In destructor you should do the opposite, i.e. releasing of derived class's resources should go before releasing resources of base. The reason is straight forward. Derived class's resource may depend on base's resource. If you release resource of base before then there might be trouble.
这适用于所有OOP。在构造函数(以及其他方法)中,您应该在代码之前调用父代的构造函数。原因是您的代码可能需要一些在父级中处理的初始化,即base的初始化应该在派生类的初始化之前进行。在析构函数中,你应该做相反的事情,即释放派生类的资源应该在释放base的资源之前。原因很简单。派生类的资源可能依赖于base的资源。如果在此之前释放基础资源可能会有麻烦。
This is the ideal case. In many cases you may see no difference but if there is dependency like described above then you will be in trouble. So you should follow the standard, call base class's method before your code and in dealloc do the opposite.
这是理想的情况。在许多情况下,您可能看不出任何差异,但如果存在如上所述的依赖性,那么您将遇到麻烦。因此,您应该遵循标准,在代码之前调用基类的方法,并且在dealloc中执行相反的操作。
#2
3
In viewDidLoad (and generally) you should go after, cause its calling the load method on the parent class
在viewDidLoad(通常)你应该去,因为它调用父类的load方法
In dealloc you'd go before
在dealloc你要去之前
#1
3
This is applicable for all OOP in general. In constructor (and in other methods too) you should call the parent's constructor before your code. The reason is your code may require some initialization which are handled in parent, i.e. initialization of base should go before initialization of derived class. In destructor you should do the opposite, i.e. releasing of derived class's resources should go before releasing resources of base. The reason is straight forward. Derived class's resource may depend on base's resource. If you release resource of base before then there might be trouble.
这适用于所有OOP。在构造函数(以及其他方法)中,您应该在代码之前调用父代的构造函数。原因是您的代码可能需要一些在父级中处理的初始化,即base的初始化应该在派生类的初始化之前进行。在析构函数中,你应该做相反的事情,即释放派生类的资源应该在释放base的资源之前。原因很简单。派生类的资源可能依赖于base的资源。如果在此之前释放基础资源可能会有麻烦。
This is the ideal case. In many cases you may see no difference but if there is dependency like described above then you will be in trouble. So you should follow the standard, call base class's method before your code and in dealloc do the opposite.
这是理想的情况。在许多情况下,您可能看不出任何差异,但如果存在如上所述的依赖性,那么您将遇到麻烦。因此,您应该遵循标准,在代码之前调用基类的方法,并且在dealloc中执行相反的操作。
#2
3
In viewDidLoad (and generally) you should go after, cause its calling the load method on the parent class
在viewDidLoad(通常)你应该去,因为它调用父类的load方法
In dealloc you'd go before
在dealloc你要去之前