18-高级运算符

81 阅读6分钟

Swift 高级运算符 (Advanced Operators)

溢出运算符(Overflow Operator)

swift和oc不一样,他在使用的过程如果出现了溢出,而且你没有使用溢出运算符,而是使用的普通的+ - * \的话就会发生致命错误崩溃,如果你不想出现这个崩溃,那么你就需要使用溢出运算符,swift有溢出运算(&+、&-、&*),用来支持溢出运算,来看看下面的代码

var min = UInt8.min
print(min &- 1)  // 255, Int8.max

var max = UInt8.max
print(max &+ 1)  // 0, Int8.min
print(max &* 2)  // 254, 等价于 max &+ max

其实溢出的这些数据就想形成了一个环,超过最大就变成最小了,小于最小又成最大了。

运算符重载

什么叫重载,是函数名相同,而功能不同,叫重载。 比方说我们平时使用的加减乘除,只能用在数字上,如果我们想用在类,结构体,枚举上就需要为现有的运算符提供自定义的实现,这就是重载

struct Point {
    var x: Int, y: Int
}

func + (p1: Point, p2: Point) -> Point {
    Point(x: p1.x + p2.x, y: p1.y + p2.y)
}
// 看这里,原来的加号运算符不能用在结构体上,这里因为上面对+号增加了一个实现,让他能处理了结构体
let p = Point(x: 10, y: 20) + Point(x: 11, y: 22)
print(p)  // Point(x: 21, y: 42)

// 当然这个方法也能也在结构体内部,这些写也是比较合理的
struct Point {
    var x: Int, y: Int
    static func + (p1: Point, p2: Point) -> Point {
        Point(x: p1.x + p2.x, y: p1.y + p2.y)
    }
}

下面列举了更多的例子

static func + (p1: Point, p2: Point) -> Point {
    Point(x: p1.x + p2.x, y: p1.y + p2.y)
}

static func - (p1: Point, p2: Point) -> Point {
    Point(x: p1.x - p2.x, y: p1.y - p2.y)
}

static prefix func - (p: Point) -> Point {
    Point(x: -p.x, y: -p.y)
}

static func += (p1: inout Point, p2: Point) {
    p1 = p1 + p2
}

static prefix func ++ (p: inout Point) -> Point {
    p += Point(x: 1, y: 1)
    return p
}

static postfix func ++ (p: inout Point) -> Point {
    let tmp = p
    p += Point(x: 1, y: 1)
    return tmp
}

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

这里解释下

  • prefix 表示这个一元运算符写在操作数前面,例如 ++p、-p。
  • postfix 表示这个一元运算符写在操作数后面,例如 p++。
  • infix 这是二元运算符,表示中缀,就是写在两个操作数的中间,例如 p1 + p2

下面将两个swift里面自带两个东西

Equatable

要想得知2个实例是否等价,一般做法是遵守 Equatable 协议,重载== 运算符,与此同时,等价于重载了 != 运算符

struct Point : Equatable {
    var x: Int, y: Int
}

var p1 = Point(x: 10, y: 20)
var p2 = Point(x: 11, y: 22)
print(p1 == p2)  // false
print(p1 != p2)  // true

Swift为以下类型提供默认的 Equatable 实现

  • 没有关联类型的枚举
  • 只拥有遵守 Equatable 协议关联类型的枚举
  • 只拥有遵守 Equatable 协议存储属性的结构体 先解释第一点,没有关联类型的枚举,就是说像下面这种
enum Direction: Equatable { case north, south, east, west }

这种枚举值本质就是数字,肯定是可以比较,系统可以帮你干了

如果你非要使用关联类型,如果关联类型遵守了Equatable,也就是说关联类型系统知道你这些关联类型,那么系统也是可以帮你比较。

再说一个类型,结构体,如果存储属性里面都是遵守了Equatable,那么系统也知道怎么比较的。

还有一点,你既然实现了==,那么就相当于你实现了 != 前面其实都是讨论的是 == ,swift中还有三个 === 的,他代表的是啥意思呢?我想比较两个person是不是同一个对象,他们的地址是不是同一个呢,可以使用三个等号======!==相反

Comparable

两个对象除了相等,不相等的关系,还有大于,小于,大于等于,小于等于。这个时间就需要用到Comparable协议了

struct Student: Comparable {
    var age: Int
    var score: Int

    init(score: Int, age: Int) {
        self.score = score
        self.age = age
    }

    static func < (lhs: Student, rhs: Student) -> Bool {
        (lhs.score < rhs.score)
        || (lhs.score == rhs.score && lhs.age > rhs.age)
    }

    static func > (lhs: Student, rhs: Student) -> Bool {
        (lhs.score > rhs.score)
        || (lhs.score == rhs.score && lhs.age < rhs.age)
    }

    static func <= (lhs: Student, rhs: Student) -> Bool {
        !(lhs > rhs)
    }

    static func >= (lhs: Student, rhs: Student) -> Bool {
        !(lhs < rhs)
    }
}

var stu1 = Student(score: 100, age: 20)
var stu2 = Student(score: 98, age: 18)
var stu3 = Student(score: 100, age: 20)
print(stu1 > stu2)  // true
print(stu1 >= stu2) // true
print(stu1 >= stu3) // true
print(stu1 <= stu3) // true
print(stu2 < stu1)  // true
print(stu2 <= stu1) // true

上面的例子的干了两件事

  1. 遵守了Comparable 协议
  2. 重载相应的运算符,即重载了大于,小于,大于等于,小于等于

自定义运算符(Custom Operator)

有时间我想自己造一些运算符出来,就是系统没有的,比方说+++

prefix operator  前缀运算符
postfix operator 后缀运算符
infix operator   中缀运算符:优先级组

precedencegroup 优先级组 {
    associativity: 结合性 (left | right | none)
    higherThan:    比谁的优先级高
    lowerThan:     比谁的优先级低
    assignment:    true 代表可选链操作中拥有跟赋值运算符一样的优先级
}

prefix operator +++
infix operator +- : PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
    associativity: none
    higherThan:    AdditionPrecedence
    lowerThan:     MultiplicationPrecedence
    assignment:    true
}

上面可以看到只有中缀运算符才需要处理优先级组,因为前缀和后缀,一个在最前面,一个在最后面,不需要处理这些。

中缀运算符因为前后都有东西,很容易遇到他的前面的操作数前面还有运算符,他后面的操作数后面还有运算符。这时就用优先级组来确定。

下面是运算符组里面的东西。

precedencegroup 优先级组 {
    associativity: 结合性 (left | right | none)
    higherThan:    比谁的优先级高
    lowerThan:     比谁的优先级低
    assignment:    true 代表可选链操作中拥有跟赋值运算符一样的优先级
}

第一个组合性啥意思呢?举个例子
a +++ b +++ c如果结合性是left 就是说 a +++ b先算,如果结合性是right,就是说b +++ c先算,如果是none的话就是没有结合性,上面的就会报错
第二个和第三个其实在确定他的优先级,在夹在那两个之间,一个大于等于,一个小于等于,正好能确定位置。那右边的值能写哪些呢,官方文档1 官方文档2

第四个需要讲一讲

infix operator +-= : PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
    associativity: none
    higherThan: AdditionPrecedence
    lowerThan: MultiplicationPrecedence
    assignment: true
}

struct Point {
    var x = 0, y = 0
}

class Person {
    var age = 0
    var point: Point = Point()
}

var p: Person? = Person()
p?.point +- Point(x: 10, y: 20)

重点讲讲最后的一句p?.point +- Point(x: 10, y: 20),这个+- 和可选链操作中拥有跟赋值运算符一样,这是啥意思呢,意思是说如果p?.point这里的point如果是nil,那么Point(x: 10, y: 20)这句不会执行,不会创建这个对象。