Swift-Protokollerweiterungen überschreiben
Ich experimentiere mit Swift-Protokollerweiterungen und fand dieses Verhalten ziemlich verwirrend. Können Sie mir helfen, das gewünschte Ergebnis zu erzielen?
Siehe die Kommentare in den letzten 4 Zeilen des Codes. (Du kannst es kopieren, um es in XCode7 Spielplatz einzufügen, wenn du willst). Vielen Dank!
//: Playground - noun: a place where people can play
import UIKit
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"