Declarations in extensions cannot override yet error in Swift 4

J. Doe picture J. Doe · Jun 18, 2017 · Viewed 29k times · Source

I have an extension:

public extension UIWindow {
    override public func topMostController()->UIViewController? { ... }
}

but for my topMostController I get the next error:

Declarations in extensions cannot override yet error

It works well for Swift 3.1, but for Swift 4 I get this error. How can it be fixed? What did they change in Swift 4?

Answer

Sulthan picture Sulthan · Oct 15, 2017

It will work if you make the base implementation @objc. See Hamish's answer for a detailed explanation about the internals.

Overriding methods declared in extensions is a bit tricky to do correctly. Objective-C supports it, but it's not absolutely safe. Swift aims to do it better. The proposal is not completed yet.

Current version of the proposal available here.