您的位置:首页 > 移动开发 > Swift

Swift自定义Class实现Hashable

2015-12-23 00:10 519 查看
假如有个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之间可以直接进行比较了。



最终代码如下

// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: