3
這是我的代碼。看來,當我繼承UIColor,使其equatable我得到一個內存錯誤。這是爲什麼?實現快速等值協議會導致訪問錯誤。爲什麼?
class MyColor: UIColor, Equatable {
var name: String
init(name: String, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1.0) {
self.name = name
super.init(red: r, green: g, blue: b, alpha: a)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func == (lhs: MyColor, rhs: MyColor) -> Bool {
return lhs.name == rhs.name
}
let test1 = MyColor(name: "coolRed", r: 10, g: 12, b: 22)
let test2 = MyColor(name: "coolBlue", r: 10, g: 12, b: 22)
if test1 == test2 {
println("hey")
}
可能無關的錯誤,但是您的代碼不是來自'MyColor任何不同(名: 「coolRed」 中,r:1,G 1,B:1)'和'MyColor(名稱:「 coolBlue「,r:1,g:1,b:1)' – nhgrif
我知道實際的顏色沒有任何不同,但與錯誤無關..這不是完美的代碼。我附上了錯誤的屏幕截圖 – hamobi
對於任何感興趣的人,只需將此問題中的代碼複製並粘貼到沙盒中即可生成相同的錯誤。 – nhgrif