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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值