6

I was having an issue with a subclass's method getting called that overrode a method, so I created a small app to test it. When the superclass calls a method that its subclass overrides, the superclass's version of the method still gets called instead of the subclass's version, which overrides the superclass's method and should be the method getting called.

Expected output: sub foo

Actual output: super foo

Superclass:

class ViewController: UIViewController
{
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        foo()
    }

    func foo()
    {
        println("super foo")
    }
}

Subclass:

class SubclassViewController: ViewController
{
    override func foo()
    {
        println("sub foo")
    }
}
3
  • What is the class of the object on which touchesBegan is called? Is it SubclassViewController or is it ViewController? Commented Aug 19, 2015 at 22:12
  • 1
    Wow, I feel stupid. I never changed the initial view controller in the storyboard to be a SubclassViewController. Thanks. Put your comment as an answer.
    – Adam Evans
    Commented Aug 19, 2015 at 22:19
  • I'm actually seeing this issue with selectors in a superclass, ios 10, Xcode 8
    – MobileMon
    Commented Sep 21, 2016 at 19:22

3 Answers 3

7

Make sure that the object's class is SubclassViewController. Otherwise, it will not have any knowledge of the method which is overriden by subclass

2
  • I just found that I was loading VCs from a storyboard by name, but using "as MyBaseName." They are specified as the correct class in the storyboard, however. The baffling thing is that for SOME of them, the overridden method is called. But not others.
    – Oscar
    Commented Apr 16, 2022 at 2:33
  • Seems Swift 4 has no such issue, where even if you type-cast, there was no way of calling super-method without calling sub-class's method first.
    – Top-Master
    Commented Aug 16, 2023 at 14:14
6

It's also worth checking that you're not trying to override in an extension. For some reason the override can't be done in an extension.

1
  • Thanks Mike, this functionality is really stupid! Can't believe it after converting from ObjC! What the heck swift..
    – TheJeff
    Commented Jan 3, 2021 at 22:54
3

I just stumbled into this issue in Xcode 8.1 / Swift 3.0

I created a super class with a method stub with the intention of overriding it in my subclass, but calling it from the super and to my surprise the override wasn't being called.

My solution to the problem was to create a protocol. Using the OP's example, my solution looks like this:

Super:

class ViewController: UIViewController
{
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        foo()
    }

}

Subclass:

class SubclassViewController: ViewController
{
    override func foo()
    {
        println("sub foo")
    }
}

Protocol:

protocol Fooizer {
   func foo() 
}

Protocol extension:

extension ViewController : Fooizer{
   func foo(){
      abort() //If you don't override foo in your subclass, it will abort
   }
}
1
  • No need for abusing protocol(s). Instead the super-method should throw "Abstract method called; override and ensure not to call super." error (check my profile for license of this quote).
    – Top-Master
    Commented Aug 16, 2023 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.