Swift Equatable

判断两个实例的值、或者实例是否相等,必须遵循 Equatable 协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class IntegerRef: Equatable {
let value: Int
init(_ value: Int) {
self.value = value
}

static func == (lhs: IntegerRef, rhs: IntegerRef) -> Bool {
return lhs.value == rhs.value
}
}

let a = IntegerRef(100)
let b = IntegerRef(100)

print(a == a, a == b, separator: ", ")
// Prints "true, true"

let c = a
print(a === c, b === c, separator: ", ")
// Prints "true, false"