1

I checked Declarations in extensions cannot override yet error in Swift 4

In my case :

I have BaseViewController like below :

class BaseViewController: UIViewController {
    func extFunction() {
        print("extFunction")
    }
}

In have another view controller

class TestViewController : BaseViewController {
    override func extFunction() { // getting error in this line
        print("extFunction from TestViewController")
    }
}

UPDATE - I have now changed it to:

class BaseViewController: UIViewController {
    func extFunction() {
        print("extFunction")
    }
}

In have another view controller

class TestViewController : BaseViewController {
    override func extFunction() { // getting error in this line
        print("extFunction from TestViewController")
    }
}

Any help would be appreciated.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Vini App
  • 7,339
  • 2
  • 26
  • 43

2 Answers2

3

The error message is pretty clear (despite the somewhat dubious grammar):

declarations from extensions cannot be overridden yet

You cannot override a function that is declared in an extension (The 'yet' implies that this may be possible in a future version of Swift).

If you want to be able to override extFunction you must declare it in BaseViewController itself, not in an extension.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I removed it from extension and declared it in the `BaseViewController`. But the error is still not going away. – Vini App Sep 20 '17 at 21:14
  • Then you haven't done something correctly. Double check or edit your question to add the updated code – Paulw11 Sep 20 '17 at 21:17
  • @ViniApp What is the error you are getting now? It cannot be the same error as you don't have an extension. The code you have now in your question works for me. – Paulw11 Sep 20 '17 at 22:02
1

A (lousy) workaround would be to declare another function in the BaseViewController

func altExtFunc() {
    print("overridable extFunction")
}

and change extFunction to

func extFunction() {
    altExtFunc()
}

then in TestViewController

override func altExtFunc() {
    super.altExtFunc()
    // more stuff here
}
Sasho
  • 3,532
  • 1
  • 31
  • 30