高级运算符

溢出运算符(Overflow Operator)

  • Swift的算数运算符出现溢出时会抛出运行时错误
  • Swift又溢出运算符(&+ &- &*)用来支持溢出
var min = UInt8.min
print(min &- 1)//255
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
print(min - 1)

var max = UInt8.max

print(max &+ 1)//0
//Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
print(max + 1)

print(max &* 2)//254
//Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
print(max * 2)

运算符重载(Operator Overload)

  • 类 枚举 结构体可以为现有的运算符提供自定义的实现,这个操作叫做:运算符重载
struct Point {
    var x: Int, y:Int
}
func +(p1: Point, p2: Point) -> Point {
    return 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)
  • 结构体内部重载运算符需要添加关键字static
struct Point {
    var x: Int, y:Int
    
    static func + (p1: Point, p2: Point) -> Point {
        return 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)
  • prefixpostfix关键字
struct Point {
    var x: Int, y:Int
    
    static func + (p1: Point, p2: Point) -> Point {
        return Point(x: p1.x + p2.x,y: p1.y + p2.y)
    }
    
    static func -(p1: Point ,p2: Point) -> Point {
        return Point(x: p1.x - p2.x, y: p1.y - p2.y)
    }
    
    static func +=(p1: inout Point,p2: Point) {
        p1 = p1 + p2
    }
    //需要添加prefix关键字
    static prefix func ++(p: inout Point) -> Point {
        p += Point(x: 1, y: 1)
        return p
    }
    //需要添加postfix关键字
    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 {
        return (p1.x == p2.x) && (p1.y == p2.y)
    }
}

Equatable

  • 要想得知2个实例是否等价,一般做法是遵守Equatable协议,重载==运算符
  • 与此同时,等价于重载了!=运算符
  • Swift为一下类型默认提供了Equatable实现
    1.没有关联类型的枚举
    2.只拥有遵守Equatable协议关联类型的枚举
    3.只拥有遵守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

Comparable

  • 要想比较两个实例大小,一般做法是遵守Comparable协议,重载相应的运算符
//score大的比较大,如果score相等,age小的比较大
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 {
        return (lhs.score < rhs.score) || (lhs.score == rhs.score && lhs.age > rhs.age)
    }
    
    static func > (lhs: Student,rhs: Student) -> Bool {
        return (lhs.score > rhs.score) || (lhs.score == rhs.score && lhs.age < rhs.age)
    }
    
    static func <= (lhs: Student, rhs: Student) -> Bool {
        return !(lhs > rhs)
    }
    static func >= (lhs: Student, rhs: Student) -> Bool {
        return !(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

自定义运算符(Custom Operator)

  • 可以自定义新的运算符:在全局作用域使用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
}

Apple参考文档1
Apple参考文档2

prefix operator +++
infix operator +- : PlusMinusPrecedence

precedencegroup PlusMinusPrecedence {
    associativity: none
    higherThan: AdditionPrecedence
    lowerThan: MultiplicationPrecedence
    assignment: true
}


struct Point {
    var x: Int, y: Int
    static prefix func +++(point: inout Point) -> Point {
        point = Point(x: point.x + point.x, y: point.y + point.y)
        return point
    }
    
    static func +-(left: Point,right: Point) -> Point {
        return Point(x: left.x + right.x, y: left.y - right.y)
    }
    
    static func +-(left: Point?,right: Point) -> Point {
        print("+-")
        return Point(x: left?.x ?? 0 + right.x, y: left?.y ?? 0 - right.y)
    }
}


struct Person {
    var point: Point
}
var person: Person? = nil
person?.point +- Point(x: 10, y: 20)

你可能感兴趣的:(高级运算符)