Skip to content

Instantly share code, notes, and snippets.

@creaaa
Last active May 16, 2017 21:42
Show Gist options
  • Save creaaa/c09c668973c8573671b8895dc7f26f11 to your computer and use it in GitHub Desktop.
Save creaaa/c09c668973c8573671b8895dc7f26f11 to your computer and use it in GitHub Desktop.
演算子の適合方法
// クラス内でなく、グローバルで宣言してもよい (ここの型、HogeじゃなくてSelfとかはできないよ)
func == (lhs: Hoge, rhs: Hoge) -> Bool {
return lhs.name == rhs.name
}
struct Hoge: Equatable {
let name: String
// もちろん、クラス宣言内で定義してもOKです
/*
static func == (lhs: Hoge, rhs: Hoge) -> Bool {
return lhs.name == rhs.name
}
*/
}
let hoge1 = Hoge(name: "hoge")
let hoge2 = Hoge(name: "hoge")
print(hoge1 == hoge2) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment