26

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?

Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
  • Possible duplicate of [Should I call \[super awakeFromNib\]?](http://stackoverflow.com/questions/3989665/should-i-call-super-awakefromnib) – new2ios Sep 17 '16 at 11:46

2 Answers2

37

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 ...
}
iHS
  • 5,372
  • 4
  • 31
  • 49
  • Thank you! Best answer with documentation link! – Artem Novichkov Jun 30 '16 at 04:20
  • Does the order not matter? Any reason why one would want to do `[super awakeFromNib]` at the end rather than at the start of your own implementation of it? – micnguyen Oct 02 '16 at 07:07
  • @micnguyen it's worth noting that you're extending the behaviour of the super method by overriding it. So considering this, super needs a chance to implement its own thing first, because otherwise your subclass could be doing stuff that is already done. I've seen devs calling super at the end, but this is not good practise, kind of defeats the purpose of a super call. – PostCodeism Nov 18 '16 at 18:22
  • @PostCodeism Yeah, I agree. But the apple docs notes: "You may call the super implementation at any point during your own awakeFromNib method." so I'm just a little curious why they don't just enforce it at the start. – micnguyen Nov 21 '16 at 01:08
  • @micnguyen because best practises shouldn't be enforced, they should only be advised :) Besides, sometimes there is actually a good reason to call it at the end...but very rarely. – PostCodeism Nov 23 '16 at 21:42
  • I can't come up with any reason to put it in the end matter how hard I try. Could you give an example? – Ostmeistro Jan 13 '17 at 14:32
20

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 ...
}
Darren Ford
  • 856
  • 5
  • 10