Vini App Vini App - 29 days ago 12
Swift Question

Declarations in extensions cannot override yet

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.

Answer Source

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.