Error overriding function in fast

I have a structure:

struct ErrorResultType: ErrorType {
    var description: String
    var code: Int
}

and protocol:

protocol XProtocol {
    func dealError(error: ErrorResultType)
}

Now I want to make a UIViewController extension:

extension UIViewController: XProtocol {

    func dealError(error: ErrorResultType) {
        // do something 
    }
}

So, I can subclass from this and override a function like:

class ABCViewController: UIViewController {

--->override func dealError(error: ErrorResultType) {
        super.dealError(error)
        // do something custom
    }
} 

But this does not happen like this: Declarations from extensions cannot be overridden yet

That makes no sense to me. When I replace everything ErrorResultTypewith AnyObject, the error will no longer be displayed.

What did I miss?

+4
source share
2 answers

Currently, a method in an extension must be marked @objcto allow its overriding in subclasses.

extension UIViewController: XProtocol {

    @objc
    func dealError(error: ErrorResultType) {
        // do something
    }
}

, Objective-C , ErrorResultType - . , ErrorResultType .

+4

, Swift .

:

, , :

class Base { }

extension Base {
    var foo: String { return "foo" }
}

class Sub: Base {
    override var foo: String { return "FOO" } // This is an error
}

, : https://github.com/ksm/SwiftInFlux/blob/master/README.md#overriding-declarations-from-extensions

+1

Source: https://habr.com/ru/post/1618387/


All Articles