Swift-高级运算符

2020-11-30  本文已影响0人  懒床小番茄

一、溢出运算符

swift当我们进行算术运算出现溢出的时候,程勋会发生运行时的报错,所以swift为我们提供了溢出运算符(&+ 、 &- 、 &*),用来支持运算的溢出。
例如:

var v1 = UInt8.max
var v2 = v1 + 4
print("v2=",v2)

我们知道UInt8.max的值是255,UInt8支持的范围是0~255,上述计算v2的值溢出了,系统报错


image.png

当我们使用溢出运算符&+的时候

var v1 = UInt8.max
var v2 = v1 &+ 4
print("v2=",v2)

计算结果就会显示正常


image.png

溢出运算符计算结果,当计算的结果大于或者小于最大值或者最小值的时候,会从头开始取值
例如上面的加法运算,结算相当于 255+4-256,减法运算相当于 减数-被减数+256,乘法运算相当于 (乘数*被乘数-256)%256

二、运算符重载

swift中,类、结构体、枚举都可以为现有的运算符提供自定义的实现,这种操作叫做运算符的重载
例如:

struct Point{
    var x = 0,y = 0
    static func + (p1:Point,p2:Point) -> Point{
        Point(x: p1.x + p2.x, y: p1.y + p2.y)
    }
}
//func + (p1:Point,p2:Point) -> Point{
//    Point(x: p1.x + p2.x, y: p1.y + p2.y)
//}
var p1 = Point(x: 10, y: 20)
var p2 = Point(x: 20, y: 15)
var p3 = Point(x: 11, y: 22)
let p4 = p1 + p2
let p5 = p1 + p2 + p3
print(p4)  //Point(x: 30, y: 35)
print(p5) //Point(x: 41, y: 57)

不建议将重载方法加载外面,例如注释的+方法,因为重载跟Point类型相关,建议写在Point类型下面,但此时值得注意的是,需要在重载方法前面加static,将其定义为类型方法。
其他方法还有:

static func - (p1: Point, p2: Point) -> Point {
    Point(x: p1.x - p2.x, y: p1.y - p2.y)
}
//prefix修饰,表明是前缀运算符
static prefix func - (p: Point) -> Point { Point(x: -p.x, y: -p.y)
}
//默认情况下p1是let类型,我们希望改变外部的p1的值,使用inout输入输出参数变为引用传递
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
}
//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 {
    (p1.x == p2.x) && (p1.y == p2.y)
}

三、Equatable 协议(比较两个实例是否相等)

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

class Person: Equatable{
    var name: String
    init(name: String) {
        self.name = name
    }
    //自定义比较规则
    static func == (lhs: Person, rhs: Person) -> Bool {
        lhs.name == rhs.name
    }
}
var p1 = Person(name: "xiao")
var p2 = Person(name: "xiao")
print(p1 == p2) //true

当class遵守了Equatable协议,可以进行p1与p2的比较是否相等,上述例子,我们自定义了当name相同的时候就认定为是相等的,所以打印结果是true。

四、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

五、自定义运算符(Custom Operator)

swift中我们可以自定义新的运算符:在全局作用域使用operator进行声明
定义规则如下:

prefix operator 前缀运算符
postfix operator 后缀运算符
infix operator 中缀运算符 : 优先级组
 //当我们定义中缀运算符的时候,后面必须跟上优先级组,优先级组的定义规则如下:
precedencegroup 优先级组 {
   associativity: 结合性(left\right\none)
   higherThan: 比谁的优先级高
   lowerThan: 比谁的优先级低
   assignment: true 代表在可选链操作中拥有跟赋值运算符一样的优先级
}

例:自定义一个 +- 的运算符

infix operator +- : PlusMinusPrecedence
 precedencegroup PlusMinusPrecedence {
    associativity: none //结合性(left\right\none) 当我们定义为none的时候,不能进行a +- b +- c的操作,只能进行a +- b 的操作
    higherThan: AdditionPrecedence //比谁的优先级高
    lowerThan: MultiplicationPrecedence //比谁的优先级低
    assignment: true
 }

Apple文档参考: phttps://developer.apple.com/documentation/swift/swift_standard_library/operator_declarations
https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID380

其中assignment: true的时候:

struct Point{
    var x = 0,y = 0
    static func +- (p1:Point,p2:Point) -> Point{
        Point(x: p1.x + p2.x, y: p1.y - p2.y)
    }
}
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是否为nil

当p?.point = Point(x: 10, y: 20)的时候,可选连会先判断p是否为nil,当为nil的时候,=后面的赋值操作不会进行;+-同理,当p为nil‘的时候,+-后面的运算操作不会进行。

上一篇 下一篇

猜你喜欢

热点阅读