swift Equatable

我來了,swift

定義

用于做值類型對(duì)比的協(xié)議。

適用范圍

遵循Equatable的類型都可以使用==或是!=符號(hào)對(duì)比判斷。這個(gè)類型包括struct/class/enum

實(shí)現(xiàn)原理

任何遵循Equatable的類型,都需要實(shí)現(xiàn)方法static func ==(lhs: Self, rhs: Self) -> Bool在這個(gè)方法內(nèi)按照指定的值和值間對(duì)比關(guān)系返回true或是false,外部表現(xiàn)為==或是!=的結(jié)果

struct Human: Equatable {
    var name: String
    var age = 5
    
    static func == (lhs: Self, rhs: Self) -> Bool {
        return lhs.name == rhs.name
    }
}

let tom = Human(name: "tom", age: 5)
let tim = Human(name: "tim", age: 6)
tom == tim // false

let man1 = Human(name: "tom", age: 5)
let man2 = Human(name: "tom", age: 6)
man1 == man2 // true

Human對(duì)Equatable方法定義為只判斷name屬性是否值相同,即使age屬性不同,man1==man2結(jié)果是true

在不同類型中的差異

  • class遵循Equatable必須要實(shí)現(xiàn)static func ==(lhs: Self, rhs: Self) -> Bool方法。但是,struct遵循Equatable可以不用實(shí)現(xiàn)static func ==(lhs: Self, rhs: Self) -> Bool方法,默認(rèn)將struct實(shí)例中所有屬性對(duì)比并返回結(jié)果
struct Dog: Equatable {
    var name: String
    var age = 0
}

let dog1 = Dog(name: "tom", age: 1)
let dog2 = Dog(name: "tom", age: 1)
dog1 == dog2  // true

let dog3 = Dog(name: "tom", age: 1)
let dog4 = Dog(name: "tom", age: 2)
dog3 == dog4  // false

struct Dog遵循Equatable協(xié)議但沒有實(shí)現(xiàn)協(xié)議方法其實(shí)例dog1與dog2實(shí)例相等,dog3與dog3實(shí)例不相等

  • 數(shù)組等容器類(數(shù)組/字典等)內(nèi)部的元素需要遵循Equatable才能實(shí)現(xiàn)一些能力,例如contains(_:)
let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]

let nameToCheck = "Kofi"
if students.contains(nameToCheck) {
    print("\(nameToCheck) is signed up!")  // Prints "Kofi is signed up!"
} else {
    print("No record of \(nameToCheck).")
}

  • swift standard library中的大部分基礎(chǔ)類型默認(rèn)已經(jīng)遵循Equatable,例如Int/Array/Dictionary/Set
  • struct重的屬性如果有不遵守Equatable,那么其實(shí)例無法用==
class MyClassNoEquatable {
    
}

struct NoEquatableStruct: Equatable {
    var mc = MyClassNoEquatable()
    
}

let nes1 = NoEquatableStruct()
let nes2 = NoEquatableStruct()
nes1 == nes2

上面代碼會(huì)報(bào)錯(cuò),提示Type 'NoEquatableStruct' does not conform to protocol 'Equatable'

協(xié)議間的關(guān)聯(lián)

Hashable協(xié)議基于Equatable協(xié)議,對(duì)于struct遵守Hashable并不需要實(shí)現(xiàn)Equatable方法,但是對(duì)于class遵守Hashable必須要實(shí)現(xiàn)Equatable的方法
Comparable協(xié)議基于Equatable

=====

  • Equatable本質(zhì)是對(duì)屬性的值對(duì)比,適用范圍有struct/class/enum
  • ===是對(duì)class實(shí)例指針的對(duì)比,且僅適用于class實(shí)例
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(c === a, c === b, separator: ", ") // Prints "true, false"

參考

Equatable
Swift Equatable

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容