简体   繁体   English

调用超类的awakeFromNib

[英]Calling awakeFromNib of superclass

I installed Xcode 8.0 beta (8S128d). 我安装了Xcode 8.0 beta(8S128d)。 Now I have some warnings with message: 现在我有一些警告信息:

Method possibly missing a [super awakeFromNib] call

in all awakeFromNib methods. 在所有awakeFromNib方法中。 In which case I need to call this method of superclass? 在这种情况下,我需要调用此超类方法?

As per Apple : 根据苹果

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. 您必须调用awakeFromNib的超级实现,以使父类有机会执行其所需的任何其他初始化。 Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. 尽管此方法的默认实现不执行任何操作,但是许多UIKit类提供了非空实现。 You may call the super implementation at any point during your own awakeFromNib method. 您可以在自己的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对此进行了更改,并且如果[super awakeFromNib]中缺少对[super awakeFromNib] (或super.awakeFromNib()调用,编译器会将其视为错误。

So Swift version would look something like this: 因此Swift版本看起来像这样:

func awakeFromNib() {
   super.awakeFromNib()

   ... your magical code ...
}

You're effectively overriding the method 'awakeFromNib' in your code. 您实际上在代码中覆盖了方法“ awakeFromNib”。 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. NSView或您的超级视图也实现了awakeFromNib -在执行任何代码之前,应在实现开始时调用超级,以确保NSView可以预先正确设置。

- (void)awakeFromNib
{
   [super awakeFromNib];

   ... your code ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM