Swift自定义Class实现Hashable

假如有个Bit类,其中含有CGPoint类型的point属性,Class定义如下

class Bit {
    var point : CGPoint
    init(point : CGPoint) {
        self.point = point
    }
}

疑问:Bit之间怎么实现比较? 答案:实现Hashable协议就可以,而Hashable实际上又需要实现Equatable协议

1.实现Hashable

当给类增加Hashable协议后,XCode编译抛出"Type 'Bit' does not conform to protocol 'Hashable'.Command+click点击Hashable的定义会发现如下代码:

  protocol Hashable : Equatable {
    /// Returns the hash value.  The hash value is not guaranteed to be stable
    /// across different invocations of the same program.  Do not persist the hash
    /// value across program runs.
    var hashValue: Int { get }
}

我们需要实现hashValue属性的getter,总所周知String类型是实现了Hashable的(String类型之间是可以直接比较,排序),所以可以利用String这点来实现Getter,如下代码:

var hashValue : Int {
    get {
        return "\(self.point.x),\(self.point.y)".hashValue
    }
}

增加代码后,发现编译器依然报错"Type 'Bit' does not conform to protocol 'Equatable'"-没有实现Equatable协议。

2.实现Equatable

Commend+click点击Hashable定义,进入后再点击Equatable协议定义,可以看到如下定义:

protocol Equatable {
    func ==(lhs: Self, rhs: Self) -> Bool
}

我们会发现Equatable协议是需要实现一个函数,即==函数,那么我们究竟怎么来实现呢?

首先,我们利用getter返回值hashable来进行比较来实现,函数实现代码如下:

func ==(lhs: Bit, rhs: Bit) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

以上实际上是实现重载运算符==在下面代码中,我们发现2个point之间可以直接进行比较了。

Swift自定义Class实现Hashable

最终代码如下

// Playground - how to implement Hashable and Equatable

import UIKit

//MARK: - Equatable
func ==(lhs: Bit, rhs: Bit) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

class Bit : Hashable {

    var point : CGPoint

    //MARK: - Hashable
    var hashValue : Int {
        get {
            return "\(self.point.x),\(self.point.y)".hashValue
        }
    }

    //MARK: - Bit
    init(point: CGPoint) {
        self.point = point
    }

}

var point_a_1_0 = Bit(point: CGPoint(x: 1, y: 0))
var point_b_1_0 = Bit(point: CGPoint(x: 1, y: 0))
var point_c_0_1 = Bit(point: CGPoint(x: 0, y: 1))

point_a_1_0 == point_b_1_0
point_a_1_0 == point_c_0_1

以上代码在XCode6 GM测试通过。

原文:http://www.swiftcoder.info/dev/codefellows/2014/8/2/how-to-implement-hashable-for-your-custom-class

posted @ 2015-12-23 00:10  brave-sailor  阅读(586)  评论(0编辑  收藏  举报