Generate super call when overwriting a method

I just filed the following suggestion via the Feedback assistant. I'm posting it here for having a URL to refer to / for public discussion. How do you think about this - would you like to have this tiny detail improvement in Xcode as well?


When using the code completion to override methods, it would be very helpful if Xcode would automatically generate a call to the super-class implementation of the method.


Steps to reproduce:


1. Generate a new iOS Project with the Single View application template.

2. Open the ViewController class.

3. Begin to type viewWillAppear… in the class to override the superclass method.


Expected:


A method stub with a call to the super method should be inserted:


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) // <- this should be here automatically
    <#code#>
}

Actual:


A method stub without a call to the super method is inserted:


override func viewWillAppear(_ animated: Bool) {
    <#code#>
}

Replies

When to call super may depend on the method, not always at the beginning or even should not be called.

For example UIView updateConstraintsdocumentation explicitly states "Call [super updateConstraints] as the final step in your implementation."

So: // <- this should be here automatically is not correct


for example

UIViewController loadView
explicitly says: "Your custom implementation of this method should not call super."

So, a warning could be useful, automatic insertion questionnable.


See here:

https://stackoverflow.com/questions/38689059/when-to-use-super-when-overriding-ios-methods


Most of the time, it's probably a safe bet that you should call the super implementation of a method (see comments below). However, be warned, some methods require you to call their super implementation, some don't, and some even require you to call the super implementation at a specific point in your override. So remember, when in doubt, always consult the documentation.

Good point, this makes this more tricky.


Although my suggestion was not so much about accidentially missing a call to super but the convenience of not having to type it manually, so a warning wouldn't help in that regard (I assume).


Maybe the <#code#> insertion marker could be left out to not imply any order.

The other cases could be handled by some way to document must-(not)-call-super cases as part of the method signature so the tooling could react accordingly.