xcode 调用超类的awakeFromNib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37804623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling awakeFromNib of superclass
提问by Artem Novichkov
I installed Xcode 8.0 beta (8S128d). Now I have some warnings with message:
我安装了 Xcode 8.0 beta (8S128d)。现在我有一些警告消息:
Method possibly missing a [super awakeFromNib] call
in all awakeFromNib
methods.
In which case I need to call this method of superclass?
在所有awakeFromNib
方法中。在什么情况下我需要调用超类的这个方法?
回答by iHS
As per Apple:
根据苹果:
You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.
您必须调用awakeFromNib 的超级实现,让父类有机会执行它们需要的任何其他初始化。尽管此方法的默认实现什么都不做,但许多 UIKit 类提供了非空实现。您可以在自己的awakeFromNib 方法中的任何时候调用超级实现。
Before Xcode 8, there was no strict compiler requirement for this , howeever Apple has changed this with Xcode8, and compiler treats it as error if call to [super awakeFromNib]
(or super.awakeFromNib()
in swift) is missing in awakeFromNib.
在 Xcode 8 之前,对此没有严格的编译器要求,但是 Apple 已经用 Xcode8 改变了这一点,如果在awakeFromNib 中缺少对[super awakeFromNib]
(或super.awakeFromNib()
在 swift 中)的调用,编译器会将其视为错误。
So Swift version would look something like this:
所以 Swift 版本看起来像这样:
func awakeFromNib() {
super.awakeFromNib()
... your magical code ...
}
回答by Darren Ford
You're effectively overriding the method 'awakeFromNib' in your code. NSView or whatever your superview is also implements awakeFromNib -- you should call the super at the start of your implementation before you do any of your code to make sure that NSView can set itself up correctly beforehand.
您有效地覆盖了代码中的“awakeFromNib”方法。NSView 或任何你的超级视图也实现了awakeFromNib——你应该在你执行任何代码之前在你的实现开始时调用超级,以确保 NSView 可以事先正确设置自己。
- (void)awakeFromNib
{
[super awakeFromNib];
... your code ...
}