肇鑫的技术博客

业精于勤,荒于嬉

`==`与`hashValue`

Hashable是最常见的协议之一。也许是由于它太常见了。以至于好多人都没法正确的实现它。下面总结一下实现它需要的原则。

基本原则

基本原则只有两条:

  1. Hashable继承了Equatable。因此,要实现Hashable必须同时实现Equatable
  2. 当相同类型的ab相等时,它们的hashValue相等。反之不成立,即hashValue相等的两个相同类型的ab,不一定相等。
class Fruit:Hashable {
    let name:String
    
    var hashValue: Int {
        return name.hashValue
    }
    
    init(name:String) {
        self.name = name
    }
    
    static func ==(lhs: Fruit, rhs: Fruit) -> Bool {
        return lhs.name == rhs.name
    }
}

注意

var hashValue: Int只要求返回值,因此如何实现都可以,设置只返回固定的值,比如var hashValue: Int { return 10 }也是可以的。
不能使用hashValue来判断是否相等。

不同的解读

上面的代码在涉及到子类时,不同的人会有不同的理解。比如:

class Apple:Fruit {
    override var hashValue: Int {
        return super.hashValue - "apple".characters.count
    }
}

class Banana:Fruit {
    override var hashValue: Int {
        return super.hashValue - "banana".characters.count
    }
}

let a = Apple(name: "")
let b = Banana(name: "")
print(a == b) // true
print(a.hashValue) // prints 4799450059485596700
print(b.hashValue) // prints 4799450059485596699

上面的代码,print(a == b) // true,但是hashValue却不相等。这违背了上面提到的基本原则2。这段代码需要修改。但是如何修改,不同的人有不同的看法。

判断类型

我认为最好的办法就是优先判断类型。因为,类型不同的类,就不应该相等。比如一个苹果就不应该和一个香蕉相等。

class Fruit:Hashable {
    let name:String
    
    var hashValue: Int {
        return name.hashValue
    }
    
    init(name:String) {
        self.name = name
    }
    
    static func ==(lhs: Fruit, rhs: Fruit) -> Bool {
        return type(of:lhs) == type(of:rhs) && lhs.name == rhs.name
    }
}

class Apple:Fruit {
    override var hashValue: Int {
        return super.hashValue - "apple".characters.count
    }
}

class Banana:Fruit {
    override var hashValue: Int {
        return super.hashValue - "banana".characters.count
    }
}

let a = Apple(name: "")
let b = Banana(name: "")
print(a == b) // false
print(a.hashValue) // prints 4799450059485596700
print(b.hashValue) // prints 4799450059485596699

即在非final的类的==里,总是先比较类型,类型相同的,再毕竟其它。这么做之后,就永远不会发生一个苹果等于一个香蕉的笑话了。但是这里还有一个问题,我们没有编写子类的==。这里暂时没有问题,但是如果遇到有其它属性的子类,就可能出现问题。

class Orange:Fruit {
    let weight:Double
    
    override var hashValue: Int {
        return super.hashValue - "orange".characters.count + Int(weight)
    }
    
    init(name: String, weight: Double) {
        self.weight = weight
        super.init(name: name)
    }
}

let o1 = Orange(name: "", weight: 0.4)
let o2 = Orange(name: "", weight: 1.2)
print(o1 == o2) // true
print(o1.hashValue) // prints 4799450059485596699
print(o2.hashValue) // prints 4799450059485596700

由于Orange没有重新定义==,比较时,直接调用了Fruit==,导致了不符合基本原则2。补救办法就是在Orange重新定义==

class Orange:Fruit {
    let weight:Double
    
    override var hashValue: Int {
        return super.hashValue - "orange".characters.count + Int(weight)
    }
    
    static func ==(lhs: Orange, rhs: Orange) -> Bool {
        return type(of:lhs) == type(of:rhs) && lhs.name == rhs.name && lhs.weight == rhs.weight
    }
    
    init(name: String, weight: Double) {
        self.weight = weight
        super.init(name: name)
    }
}

let o1 = Orange(name: "", weight: 0.4)
let o2 = Orange(name: "", weight: 1.2)
print(o1 == o2) // false
print(o1.hashValue) // prints 4799450059485596699
print(o2.hashValue) // prints 4799450059485596700

不重写hashValue,仅重新定义==

一些特殊的情况下,也许你可以不重写hashValue,而仅重新定义==

import Foundation
import Cocoa

class Fruit:Hashable {
    let name:String
    
    var hashValue: Int {
        return name.hashValue
    }
    
    init(name:String) {
        self.name = name
    }
    
    static func ==(lhs: Fruit, rhs: Fruit) -> Bool {
        return lhs.name == rhs.name
    }
}

class Apple:Fruit {

}

class ColoredApple:Apple {
    let color:NSColor
    
    init(name:String, color:NSColor) {
        self.color = color
        super.init(name: name)
    }
    
    static func ==(lhs: ColoredApple, rhs: ColoredApple) -> Bool {
        return lhs.name == rhs.name && lhs.color == rhs.color
    }
}

let greenApple = ColoredApple(name: "green apple", color: .green)
let apple = Apple(name: "green apple")
print(greenApple == apple) // true
print(greenApple.hashValue) // -2580839601755588497
print(apple.hashValue) //-2580839601755588497

var set:Set<Apple> = [greenApple, apple]
print(set.count) // 1

var d:Dictionary<Apple, Int> = [greenApple:10]
// eat one
d.updateValue(d[greenApple]! - 1, forKey: apple)
print(d[greenApple]!) // 9

这种做法不常见,一旦选择了不重写hashValue,就必须在该基类的所有子类都保持一致。

注意

由于Set和Dictionary使用hashValue进行判断,放入其中的元素的hashValue在没有从其中移出时,不能改变,否则会出现问题。
可以看到applegreenApple因为hashValue相同,它们在Set和Dictionary里被当做是同一元素/键值。

`Self`和`type(of:)`的用法

在讲解Self之前,需要先简短介绍一下type(of:)的用法。

type(of:)

从Xcode 8 beta 6开始,dynamicType被替换为了type(of:)。(SE-0096)

type(of:)的功能是获得实例在运行时(runtime)的元类型(meta type)。这个在之前的Swift里,被称作是dynamicType,与静态的类型进行区别。

Self

Self是一个特殊的类型。它是self对应的类型。而self是动态类型。比如:

class Foo {
    func printType() {
        print(type(of:self))
    }
    
    func printSelf() {
        print(self)
    }
}

class Bar:Foo {
    
}

let foo = Foo()
foo.printType() // Foo
foo.printSelf() // Foo

let bar = Bar()
bar.printType() // Bar
bar.printSelf() // Bar

var test = Foo()
test.printType() // Foo
test.printSelf() // Foo

test = Bar()
test.printType() // Bar
test.printSelf() // Bar

可以看到,self始终对应的是实际类型,无论变量的类型是Foo还是Bar。

由于这个特性的存在,这使得Self无法从静态类型进行转换,而只能通过self来生成。即:

class Foo {
    func bar() -> Self {
        return self
    }
}

class Foo1 {
    func bar1() -> Self {
        return type(of:self).init()
    }
    
    required init() { }
}

注意

Foo1中,有一个required init(),这是type(of:self).init()必须的。

Self的使用有以下方式:

1. 在协议的返回值中使用

这是最常见的使用方式。

protocol Foo {
    func instance() -> Self
}

class Bar: Foo {
    func instance() -> Self {
        return self // Declaration: let `self`: Self
    }
    func other() {
        let i = self // Declaration: let `self`: Bar
    }
}

class otherBar:Foo {
    func instance() -> Self {
        return type(of:self).init()
    }
    
    required init() { }
}

####注意
Bar中的实现协议的函数里,self是Self类型。而非Self返回值的函数里,self是所在类的类型。
可以使用type(of:self).init(),获得一个新的Self实例。此时,类中必须要有一个required init()
Self作为函数的返回值的类型时,可以直接写在类中,而不必非要有协议。
下面2和3中的情形,使用associateType的效果,要好于Self。因此,1是Self的最常用的方式。


2. 在协议的函数的参数类型中使用

protocol Foo {
    func bar(b:Self)
}

class Bar:Foo {
    func bar(b:Bar) {
        print(b)
    }
}

####注意
Bar中的func bar(b:Bar)是Bar而不是Self。但是这满足协议。这也符合类的多态,即Bar的子类可以调用这个函数。


3. 在协议的属性里使用

protocol Foo {
    var bar:Self { get set }
}

final class Bar:Foo {
    var bar: Bar
    
    init(b:Bar) {
        self.bar = b
    }
}

####注意
在协议的属性里使用Self时,类必须时final类。


###参考文献:
以下参考文献的一些代码针对的是3.0版本之前的Swift。部分内容需要修正才能运行。但基本原理是相同的。

Types and meta types in swift
Swift中你应该知道的一些有用的tips
Generic Protocols & Their Shortcomings 这篇难度较高,慎看。
Self in Protocol and class method

Realm的坑(三)

上一个坑里,Realm的坑(二),我们使用非受管对象避开必须使用写入交易的问题。但是,每次使用时都要设置属性还是挺麻烦的。我们寻求一种可以一劳永逸的方式。

最初的想法

我最初想到的是利用NSCopying协议,然后利用Object对象的properties属性给Object实例的属性赋值。如:

extension Object:NSCopying {
    public func copy(with zone: NSZone? = nil) -> Any {
        let o = Object()
        for p in objectSchema.properties {
            let value = self.value(forKey: p.name)
            switch p.type {
            case .linkingObjects:
                break
            default:
                o.setValue(value, forKey: p.name)
            }
        }
        
        return o
    }
}

由于NSCopying的返回值是Any,因此,在使用时需要转换,像这样:

let anotherBar = bar.copy() as! Foo
  
try! realm.write {
    realm.add(anotherBar, update:true)
}

但是这里发生了一个问题。当前版本的Realm中有一个错误,不能在swift中直接创建Object(),程序会崩溃。因此我提交了一个issue

进阶

既然不能直接使用Object(),我决定使用通用类型的函数,像这样:

func unmanagedCopy<T>(of i:T) -> T where T:Object {
    let o = T()
    for p in i.objectSchema.properties {
        let value = i.value(forKey: p.name)
        switch p.type {
        case .linkingObjects:
            break
        default:
            o.setValue(value, forKey: p.name)
        }
    }
    
    return o
}

let anotherBar = unmanagedCopy(of: bar)
  
try! realm.write {
    realm.add(anotherBar, update:true)
}

这么做,带来一个好处,就是我在具体使用的时候不用进行类型转换了。缺点就是这是一个全局函数,也许放到一个struct里会更好一些?

最终的解决方案

在上面提单的issue里,**JadenGeller**给了我两个很好的建议。下面的代码是最终的解决方案。

// MARK: - UnmanagedCopy Protocol
protocol UnmanagedCopy {
    func unmanagedCopy() -> Self
}

extension Object:UnmanagedCopy{
    func unmanagedCopy() -> Self {
        let o = type(of:self).init()
        for p in objectSchema.properties {
            let value = self.value(forKey: p.name)
            switch p.type {
            case .linkingObjects:
                break
            default:
                o.setValue(value, forKey: p.name)
            }
        }
        
        return o
    }
}

首先建立一个UnmanagedCopy的协议。虽然直接写这个函数也可以,但是建立协议可以使函数的目的更加明确。

然后在Object对象的扩展里实现了这个协议。这里用let o = type(of:self).init()避开了不能使用Object()的问题,变量o的类型是Self。函数返回值类型Self可以确保最终的类型与self的实际类型相同,这就保证了使用时不必再进行二次转换。具体使用:

let anotherBar = bar.unmanagedCopy()
  
try! realm.write {
    realm.add(anotherBar, update:true)
}

这个函数已经基本够用了,它包含你的实例里所有持久性数据的属性,但是不包括ignore函数里包含的属性public class func ignoredProperties() -> [String]。所以,如果你有额外的需求,就应该在Object的子类里重写这个函数。如:

class Foo:Object {
    dynamic var id:Int = 0
    dynamic var name:String = ""
    dynamic var age:Int = 8
    
    var temp = "temp value"
    
    override class func ignoredProperties() -> [String] {
        return ["age", "temp"]
    }
    
    override class func primaryKey() -> String? {
        return "id"
    }
    
    override func unmanagedCopy() -> Self {
        let o = type(of:self).init()
        for p in objectSchema.properties {
            let value = self.value(forKey: p.name)
            switch p.type {
            case .linkingObjects:
                break
            default:
                o.setValue(value, forKey: p.name)
            }
        }
        
        o.age = age
        o.temp = temp
        
        return o
    }
}

有关Self的更多用法,看Self的用法

相关

Realm的坑(一)
Realm的坑(二)
Realm的坑(三)
Realm的坑(四)