定訂可以比較的型別

需遵從Comparable protocol, 並且定義 == & < 。 因為Comparable 繼承 Equatable, 所以也需要定義 == 。

struct Point: Comparable {
    let x:Int
    let y:Int
}

func == (p1:Point, p2:Point) -> Bool {
    return (p1.x == p2.x) && (p1.y == p2.y)
}

func < (p1:Point, p2:Point) -> Bool {
    return (p1.x < p2.x) && (p1.y < p2.y)
}

results matching ""

    No results matching ""