我正在尝试使用Swift协议扩展,但我发现这种行为相当令人困惑。你能帮我知道如何获得我想要的结果吗?
请查看代码的最后4行的注释。(如果需要,您可以将其复制粘贴到Xcode7 playground中)。谢谢!
protocol Color { }
extension Color { var color : String { return "Default color" } }
protocol RedColor: Color { }
extension RedColor { var color : String { return "Red color" } }
protocol PrintColor {
func getColor() -> String
}
extension PrintColor where Self: Color {
func getColor() -> String {
return color
}
}
class A: Color, PrintColor { }
class B: A, RedColor { }
let colorA = A().color // is "Default color" - OK
let colorB = B().color // is "Red color" - OK
let a = A().getColor() // is "Default color" - OK
let b = B().getColor() // is "Default color" BUT I want it to be "Red color"
A
和A.text
是"Hello"
。但是,类A:Protocol
会导致A.text
变为"Ciao"
吗? - VojtaStavikextension Lang where Self is Lang & Italian{print("Ciao")}
- Sentry.co