[Swift]オブジェクトの比較 演算子のoverride? 2015/05/09

Swiftです。
クラス定義して、生成したオブジェクトを比較して、プロパティが同じならtrueとしたいなーと考えたのでした。

isEualをクラスにオーバーライド定義すればいいのかなとか思ってたのですが、
いつものスタックオーバーフローで、なんとなくそれっぽいものがあったのでメモ。


class MyType { var p:Int = 0 init() {} init(p:Int) { self.p = p } } // 比較演算子の定義 func == (this: MyType, that: MyType) -> Bool { return this.p == that.p } if MyType(p:0) == MyType(p:0) { println("eq") // print this } else { println("not eq") } if MyType(p:1) == MyType(p:0) { println("eq") } else { println("not eq") // print this }

: