-3

My code:

extension UIViewController {
    @IBAction func someFunc() {
        ...
    }
}

class CustomViewController: UIViewController {
    @IBAction override func someFunc() {
        ...
    }
}

Can CustomViewController.someFunc() call UIViewController.someFunc()?

There are similar questions but my is different. NOTE: the original method is declared in extension not parent class!

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49

1 Answers1

2

You simply need to call super.someFunc().

class CustomViewController: UIViewController {
    @IBAction override func someFunc() {
        // Call super method
        super.someFunc()
        // Do override specific stuff
        ...
    }
}

P.S.: be aware that you forgot to inherit from UIViewController in your example.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • Read the question again. My question is not a duplicate of other questions about inheritance. If it is not possible to call the overriden extension method then write about it please. Otherwise it is not an answer – Vyachaslav Gerchicov Feb 06 '19 at 15:45
  • @VyachaslavGerchicov it doesn't method where the original method is declared. However, the method must be inherited, otherwise there's no overriding taking place. So is your `CustomViewController` supposed to be a subclass of `UIViewController` or not? You don't actually mention anything about inheritance, but your code only makes sense and compiles when inheritance takes place, so rather refine your question than say I misread it :) – Dávid Pásztor Feb 06 '19 at 15:51
  • 4
    @VyachaslavGerchicov: If CustomViewController does not inherit from UIViewController then in what sense does it “override” a method from that class? – Martin R Feb 06 '19 at 15:51
  • @DávidPásztor @MartinR sorry, missed it. Thanks. But `You last voted on this answer 17 hours ago. Your vote is now locked in unless this answer is edited.` – Vyachaslav Gerchicov Feb 07 '19 at 08:59