Swift -- 组合赋值运算符

时间:2021-03-18 01:35:51

struct CenterPointer{

    var x = 0, y = 0

}

func + (left:CenterPointer, right:CenterPointer) -> CenterPointer{

    return CenterPointer(x:left.x + right.x, y:left.y + right.y)

}

let zPointer = CenterPointer(x:2, y:3)

let yPointer = CenterPointer(x:5, y:3)

let xPointer = zPointer + yPointer

prefix func - (cPointer:CenterPointer) -> CenterPointer{

    return CenterPointer(x:-cPointer.x, y:-cPointer.y)

}

let zzPointer = CenterPointer(x:2, y:3)

let yyPointer = -zPointer

func += (inout left:CenterPointer, right: CenterPointer){

    left = left + right

}

var zzzPointer = CenterPointer(x:2, y:1)

zzzPointer += zzzPointer

func == (left:CenterPointer, right:CenterPointer) -> Bool{

    return (left.x == right.x) && (left.y == right.y)

}

func != (left:CenterPointer, right:CenterPointer) -> Bool{

    return !(left == right)