Search code examples
iosswiftswift4extension-methods

Declarations from extensions cannot be overridden yet in Swift 4


I have recently migrated my code to Swift 4. There is an issue that I am facing with extensions, i.e.:

Declarations from extensions cannot be overridden yet

I have already read multiple posts regrading this issue. But none of them entertains the scenario described below:

class BaseCell: UITableViewCell
{
    //Some code here...
}

extension BaseCell
{
    func isValid() -> String?
    {
        //Some code here...
    }
}

class SampleCell: BaseCell
{
    //Some code here...

    override func isValid() -> String? //ERROR..!!!
    {
        //Some code here...
    }
}

According to Apple,

Extensions can add new functionality to a type, but they cannot override existing functionality.

But in the above scenario, I am not overriding the method isValid() in extension. It is overridden in the SampleCell class definition itself. Still, it is giving the error.


Solution

  • But in the above scenario, I am not overriding the method isValid() in an extension.

    isValid gets declared in an extension.

    The error pretty much says that if a function is declared this way, it cannot be overridden.

    The statement is valid for both from an extension and in an extension.