2017-03-18 147 views
3

我試圖在equalityClass中實現Equatable協議,但顯示成員操作符'=='必須至少有一個類型爲'eqaualityClass'的參數 .can任何人解釋什麼是錯誤的?成員操作符'=='必須至少有一個參數類型

protocol Rectangle: Equatable { 

    var width: Double { get } 
    var height: Double { get } 

} 

class eqaualityClass:Rectangle{ 

    internal var width: Double = 0.0 
    internal var height: Double = 0.0 

     static func == <T:Rectangle>(lhs: T, rhs: T) -> Bool { 
      return lhs.width == rhs.width && rhs.height == lhs.height 
    } 
} 
+0

我覺得這回答了你的問題:會員操作「%」必須有類型的至少一個參數「視圖控制器」(HTTP://計算器。 COM /問題/ 40932230 /成員運營商必須具備的,在-至少一參數的-的型視圖控制器) – leanne

回答

2

您需要使您的Rectangle協議成爲一個類。試試這樣:

protocol Rectangle: class, Equatable { 
    var width: Double { get } 
    var height: Double { get } 
} 

class Equality: Rectangle { 
    internal var width: Double = 0 
    internal var height: Double = 0 
    static func ==(lhs: Equality, rhs: Equality) -> Bool { 
     return lhs.width == rhs.width && rhs.height == lhs.height 
    } 
} 

protocol Rectangle: class, Equatable { 
    var width: Double { get } 
    var height: Double { get } 
} 

extension Rectangle { 
    static func ==(lhs: Self, rhs: Self) -> Bool { 
     return lhs.width == rhs.width && rhs.height == lhs.height 
    } 
} 

class Equality: Rectangle { 
    internal var width: Double = 0 
    internal var height: Double = 0 
} 
+0

感謝它的worked.can請你解釋爲什麼使用class關鍵字? – adarshaU

+1

請查看http://stackoverflow.com/a/41970266/2303865 –

相關問題