Swift2 中的一组数组

Set of arrays in Swift2

嘿,我正在尝试制作一组​​数组,这些数组要容纳 2 个整数

当我做的时候

var points = Set<Array<Int>>();

我收到这个错误:

Array<Int> does not conform to protocol hashable

我正在尝试存储一堆点,例如 [x,y] ie. [33, 45]

我不想使用二维数组,因为点的顺序无关紧要,我希望能够按值删除点 (Set.remove[33,45])

在这种情况下不要使用数组,而是尝试创建一个 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 0