Swift自定义Class实现Hashable
2015-12-23 00:10
519 查看
假如有个Bit类,其中含有
疑问:Bit之间怎么实现比较? 答案:实现
我们需要实现hashValue属性的
增加代码后,发现编译器依然报错
我们会发现Equatable协议是需要实现一个函数,即
首先,我们利用getter返回值hashable来进行比较来实现,函数实现代码如下:
以上实际上是实现重载运算符
![](http://img.swiftmi.com/topicimage/201409/13_1411134947023_783.png)
最终代码如下
以上代码在XCode6 GM测试通过。
原文:http://www.swiftcoder.info/dev/codefellows/2014/8/2/how-to-implement-hashable-for-your-custom-class
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之间可以直接进行比较了。
![](http://img.swiftmi.com/topicimage/201409/13_1411134947023_783.png)
最终代码如下
// 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
相关文章推荐
- swift开发之optional类型的理解和使用
- 再议Swift操作符重载
- Swift 数组
- iOS swift中导入OC第三方
- iOS CoreData(Swift版本)
- Swift Explore - 关于 Swift 中的 isEqual 的一点探索
- iOS -- dismissViewControllerAnimated(Swift)
- swift控件之旅之UITextField
- swift 8583报文组装库
- 【XCode】Xcode7.2(7C68)下Swift基于SpriteKit出现Invalid Request: requesting subtype without specifying idiom
- Swift绘图
- swift 获取类的所有属性、获取对象的属性值、设置对象的属性值
- swift纯代码实现UITableview总结一
- 项目开发笔记-2015.12.22-swift
- Swift学习笔记(2):willSet与didSet
- swift 学习记录(继承)
- swift --2
- Swift如何实现代理,block传值
- swift字符串相关用法速查表
- swift 属性