自定义运算符

优质
小牛编辑
126浏览
2023-12-01

自定义运算符

struct Vector2D {
    var x = 0.0
    var y = 0.0
}

infix operator +++

extension Vector2D {
    static func +++ (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y + right.y)
    }
}

let v1 = Vector2D(x: 10, y: 11)
let v2 = Vector2D(x: 11, y: 10)

print(v1 +++ v2)

输出结果

Vector2D(x: 21.0, y: 21.0)