嘿,我正在做一组数组,数组包含两个整数。
当我这么做
var points = Set<Array<Int>>();我知道这个错误:
Array<Int>不符合hashable协议
我试着存储一些像[x,y] ie. [33, 45]这样的点
我不想使用2d数组,因为点的顺序并不重要,我希望能够通过值删除点(Set.remove[33,45])
发布于 2016-02-14 18:42:48
与其在这种情况下使用数组,不如尝试创建一个struct
struct Point: Hashable {
var x: Int
var y: Int
var hashValue: Int {
get {
return (31 &* x) &+ y
}
}
}
// Hashable inherits from Equatable, so you need to implement ==
func ==(lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
var set = Set<Point>()
set.insert(Point(x: 33, y: 45))
print(set.count) // prints 1
set.remove(Point(x: 33, y: 45))
print(set.count) // prints 0https://stackoverflow.com/questions/35395512
复制相似问题