3

When should I call the parent version of a function? Should it be the first thing in the child function or the last thing? (And by "parent" and "child", I mean the inheritance hierarchy, not the view hierarchy.)

This question can be answered in general, but I personally came across it when implementing view controller functions in iOS. So answers specific to iOS View Controllers programming are also relevant here.

For example:

class ChildViewController: ParentViewController {
    override func viewDidLoad() {
        // option 1 to do function's job
        super.viewDidLoad()
        // option 2 to do function's job
    }
}

I always thought option 1 was better (no reason, just a feeling!), but recently, I was writing some code which made me choose option 2.

Thanks in advance.

Edit: In my case, two child classes inherit from a single parent class. Both children have a UIBarButton which should be disabled when view loads. I wanted to put the disabling code in parent view controller (because it's the exact line of code for both self.navigationItem.rightBarButtonItem?.enabled = true) but the buttons are different so I have to create the buttons at each child view controller. This way, I have to call parent's viewDidLoad() after creating the button.

Should I change my code (and copy that line of code) or my approach is correct?

Behdad
  • 941
  • 12
  • 23
  • It has a lot of answers ig (http://stackoverflow.com/questions/9234810/what-is-the-correct-sequent-for-calling-super-viewwillappear-super-viewdidloa)(http://stackoverflow.com/questions/9234810/what-is-the-correct-sequent-for-calling-super-viewwillappear-super-viewdidloa) (http://stackoverflow.com/questions/3797557/when-programming-ios-viewcontrollers-should-you-call-parent-class-methods-before)(http://stackoverflow.com/questions/3797557/when-programming-ios-viewcontrollers-should-you-call-parent-class-methods-before) or (http://stackoverflow.com/questions/3906704/when-should-i-call-super)(http – Alexey Kubas Oct 06 '15 at 11:55

3 Answers3

4

As far as viewDidLoad and similar methods are concerned you should make call to super implementation at the very beginning of the method because you want to have everything already setup before your own code starts working

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • So my "feeling" was somehow right. Can you provide an example where not calling parent's `viewDidLoad` at the beginning causes a problem? Also I edited my question and added my exact situation. – Behdad Oct 06 '15 at 12:02
  • I cannot provide you an example. In most cases you won't even feel any difference I think. But sometimes it may lead to errors which are very difficult to track down. So you should always call super implementation – Andrey Chernukha Oct 06 '15 at 12:07
2

This question can be answered in general.

No, that depends on the use case.

Sometimes you want to call the original implementation at the beginning, sometimes at the end. In other cases you have to perform something before and after the overridden method.

It's also quite common not to call the overridden implementation at all. A good example for this case is the loadView method of UIViewController, when setting up views programmatically.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
0

Per Apple Documentation, call to super depends whether or not you want your parent class properties to set before you start using them:

Example where it is used first:

class Bicycle: Vehicle {
    override init() {
        super.init()
        numberOfWheels = 2
    }
}

Example where it is used in the end:

class RecipeIngredient: Food {
    var quantity: Int
    init(name: String, quantity: Int) {
        self.quantity = quantity
        super.init(name: name)
    }
    override convenience init(name: String) {
        self.init(name: name, quantity: 1)
    }
}

As for your view controller's delegate method calls are concerned, we typically want to initialize everything in parent class and then start building on top of it in child class.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • 1
    The question is not about `init` methods, which are quite special regarding order and calls to super (in Swift). – Nikolai Ruhe Oct 06 '15 at 11:59
  • Since this is a generic question, I tried taking few examples from Apple documentation exemplifying both the cases. I do not see a reason why `super` with context to `init` does not make sense here. Crux is to convey the idea that in Swift super can be called before or after child class sets its own properties. – Abhinav Oct 06 '15 at 12:05
  • My concern was that the special behavior of `init`, where the compiler enforces order of initialization, distracts from the generic behavior in normal methods. Also, you are not correct when saying that in Swift subclasses can setup *their own properties* in any order. They have to be set up **before** the call to super. – Nikolai Ruhe Oct 06 '15 at 12:12