Equatable
Equatable 协议的定义:
Equatable 协议的定义:protocol Equatable {
static func == (lhs: Self, rhs: Self) -> Bool
}作用:
示例:
struct Person: Equatable {
var name: String
var age: Int
static func == (lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age
}
}
let person1 = Person(name: "Alice", age: 25)
let person2 = Person(name: "Alice", age: 25)
let person3 = Person(name: "Bob", age: 30)
print(person1 == person2) // true
print(person1 == person3) // falseLast updated