Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling awakeFromNib of superclass

I installed Xcode 8.0 beta (8S128d). Now I have some warnings with message:

Method possibly missing a [super awakeFromNib] call

in all awakeFromNib methods. In which case I need to call this method of superclass?

like image 233
Artem Novichkov Avatar asked Jun 14 '16 06:06

Artem Novichkov


People also ask

When awakeFromNib is called?

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.

When to use awakeFromNib?

Typically, you implement awakeFromNib for objects that require additional set up that cannot be done at design time. For example, you might use this method to customize the default configuration of any controls to match user preferences or the values in other controls.


2 Answers

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.

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.

So Swift version would look something like this:

func awakeFromNib() {
   super.awakeFromNib()

   ... your magical code ...
}
like image 81
iHS Avatar answered Oct 13 '22 01:10

iHS


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.

- (void)awakeFromNib
{
   [super awakeFromNib];

   ... your code ...
}
like image 20
Darren Ford Avatar answered Oct 12 '22 23:10

Darren Ford