gpt4 book ai didi

clang - Swift:声明 'description' 不能覆盖多个父类(super class)声明

转载 作者:IT王子 更新时间:2023-10-29 05:35:40 27 4
gpt4 key购买 nike

我有一个类似于下面的继承结构。我正在采用 Printable 协议(protocol)并努力覆盖 description 属性。我遇到了一个谷歌此时似乎不知道的奇怪错误,提示为第三类,并引用了第二类和第一类。

雪上加霜的是,下面的代码实际上编译得很好,但我的完整代码却没有。注释掉 Second 和 Third 的属性解决了问题,代码编译,测试通过等。

Swift 继承章节对此提供了指示。

有谁知道这是什么意思,什么情况会触发它?

/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())

最佳答案

和我一样的问题,解决了:

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

override var description: String {
return customDescription()
}

因此您可以根据需要多次重写函数

关于clang - Swift:声明 'description' 不能覆盖多个父类(super class)声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459441/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com