Swift: declaration 'description' cannot ov

2019-04-06 06:36发布

问题:

I have a structure of inheritance similar to the one below. I'm adopting the Printable protocol and diligently override description property. I have encountered a bizarre error that seems to be unknown to Google at this point in time, that is prompted for the Third class, and references Second and First class.

To add insult to injury, the code below actually compiles fine, but my full code does not. Commenting the properties out on Second and Third solves the problem and the code compiles, tests pass etc.

Swift inheritance chapter provides pointers to this.

Does anyone know what it means and which circumstances trigger it?

/Users/ivanhoe/Dropbox/swift/convergence/Processable.swift:124:18: error: declaration 'description' cannot override more than one superclass declaration override var description : String { ^ /Users/ivanhoe/Dropbox/swift/convergence/Processable.swift:85:18: note: overridden declaration is here override var description : String { ^ /Users/ivanhoe/Dropbox/swift/convergence/Processable.swift:28:18: note: overridden declaration is here override var description : String {

import Foundation

class First : NSObject, Printable {

    override var description : String {
        return "first"
    }
}

class Second : First {

    override var description : String {
        return "second"
    }

}

class Third : Second {

    override var description : String {
        return "third"
    }

}

println(Third())

回答1:

Same problem for me, solved by doing:

func customDescription() -> String {
    return ""
}

override var description: String {
    return customDescription()
}

so you can override function as many times as you want to



回答2:

What is the Printable Protocol? ;-)

You could use a Getter of a property as

var description: String {
    get {
        return "This is a test";
    }
}


回答3:

I don't have access to my Mac now, but I would try removing the override in First. override is needed when subclassing but not when implementing a protocol.



回答4:

I had the same problem with SKTiled when overriding SKTiledScene. In my situation I wanted to override a mouse event for OS X.

The error was as follows:

Declaration 'mouseUp(with:)' cannot override more than one superclass declaration

The problem here is that the mouse events are declared in a class extension:

#if os(macOS)
extension SKTiledScene {

    override open func mouseDown(with event: NSEvent) {}

    override open func mouseMoved(with event: NSEvent) {
        guard let cameraNode = cameraNode else { return }
        cameraNode.mouseMoved(with: event)
    }
    override open func mouseUp(with event: NSEvent) {}
    override open func mouseEntered(with event: NSEvent) {}
    override open func mouseExited(with event: NSEvent) {}

    override open func scrollWheel(with event: NSEvent) {
        guard let cameraNode = cameraNode else { return }
        cameraNode.scrollWheel(with: event)
    }
}
#endif

I could solve my issue to override the method in my own SKTiledScene subclass by re-implementing the extension:

extension SKTiledScene {
    open override func mouseUp(with event: NSEvent) {
        try! SceneManager.shared.transitionToScene(MainMenuScene.self, withAnimation: .fade)
    }
}

I do wonder if I can be sure my extension is always loaded instead of the one from the SKTiled framework, but it seems to work well for now.



标签: clang swift