Why the call to super is the last thing on this method and not the first thing?

会有一股神秘感。 提交于 2020-03-05 04:59:07

问题


I am learning Swift as I go.

I found functions like this viewDidLoad() and many others are sometimes written like this:

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.
}

and sometimes written like this:

override func viewDidLoad() {
  // Do any additional setup after loading the view, typically from a nib.
  super.viewDidLoad()
}

I mean with the call to super.viewDidLoad() as the first thing or as the last thing inside the function?

What difference it makes to the program itself?


回答1:


First off, the docs don't say that you need to call super. Contrast that to e.g. viewWillAppear(_:), whose docs state:

If you override this method, you must call super at some point in your implementation.

Thus, calling super is only needed if you have a custom subclass of UIViewController, let's call it BaseViewController, which does something in viewDidLoad().

When you then inherit from BaseViewController, you probably need to call super to do the work of BaseViewController. Depending on the nature of it, you'd need to do it at the beginning, somewhere in the middle, or at the end of your subclass's viewDidLoad(). This is up to this BaseViewController to document it properly.

Here's a contrived example:

class ColorfulViewController: UIViewController {
    /// Defines background color of view. Override in subclass.
    var shinyColor: UIColor = .red

    /// Sets the background color. Make sure to call `super` in subclass.
    func viewDidLoad() {
        backgroundColor = shinyColor
    }
}

class PinkViewController: ColorfulViewController {
    override var shinyColor: UIColor = .systemPink

    func viewDidLoad() {
        super.viewDidLoad() // This one doesn't matter where it goes.
        // Additional work.
    }
}

Another contrived example where you'd want to call super at the end:

class CountingViewController: UIViewController {
    var count: Int = 0

    /// Counts the subviews. When subclassing, make sure to call `super` *after*
    /// view is fully configured.
    func viewDidLoad() {
        count = view.subviews.count
    }
}

class FooViewController: CountingViewController {
    override var shinyColor: UIColor = .systemPink

    func viewDidLoad() {
        // Additional work.
        super.viewDidLoad()
    }
}



回答2:


It depends. Usually for viewDidLoad() you would put the super call at the beggining of the method, because there is not really a reason to not do so.

You would have to understand the concept of super calls to understand it better.
super.someFunction() calls a function of the super class. In our case, your custom View Controller's class, let's say MyViewController is UIViewController, which is where viewDidLoad() is originally defined. In this case where you override the method, you should call the super call to make sure nothing is left behind. Note that in viewDidLoad() specifically it is not mandatory to call the super from UIViewController, although it is advised as a defensive call. Usually the documentation mention it is required or not.

If you had another custom view controller, let's say MySecondViewController which subclassed from MyViewController, and you wanted to perform some actions before the viewDidLoad() from MyViewController took place, you can position the super.viewDidLoad() of MySecondViewController at the end of the functions, or where you desire, like so:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        doSomething()
    }
}

class MySecondViewController: MyViewController {

    override func viewDidLoad() {
        // Do something else before doSomething() is called by the superclass
        doSomethingElse()
        super.viewDidLoad()
    }
}


来源:https://stackoverflow.com/questions/59447532/why-the-call-to-super-is-the-last-thing-on-this-method-and-not-the-first-thing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!