Swift 高级运算符 ⑬

2020-08-07  本文已影响0人  Aliv丶Zz

1. 溢出运算符

print(Int8.min)//-128
print(Int8.max)//127

print(uint8.min)//0
print(uint8.max)//255
var v : Int8 = Int8.max
//v = v+1    // 运行时报错
v = v &+ 1
print(v)  //-128


var v2 : Int8 = Int8.min
v2 = v2 &- 1
print(v2)//127

var v3 : uint8 = uint8.max
v3 = v3 &* 2
print(v3)//254

2. 运算符重载

  类、结构体、枚举可以为现有的运算符提供自定义的实现,这个操作就叫做:运算符重载

//运算符重载

struct Point:CustomStringConvertible {
    var x: Int8 = 0, y: Int8 = 0
   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 prefix func - (p1:Point) -> Point{
        return  Point(x: -p1.x, y: -p1.y)
    }

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

    static prefix func ++ (p: inout Point) -> Point{
        p = p + Point(x: 1, y: 1)
        return  p
    }
    static postfix func ++ (p: inout Point) -> Point{
        let temp = p
        p += Point(x: 1, y: 1)
        return  temp
    }
    
    
    
    var description: String{
        
        return "Point.x = \(x)  Point.y = \(y)"
    }
}

var p1 = Point(x: 10, y: 10)
var p2 = Point(x: 20, y: 20)

print(p1 + p2)//Point.x = 30  Point.y = 30
print(p1 - p2)//Point.x = -10  Point.y = -10

print(-p1)//Point.x = -10  Point.y = -10

p1 += p2
print(p1)//Point.x = 30  Point.y = 30

var p3 = Point(x: 10, y: 10)
var p4 = Point(x: 20, y: 20)
var p5 = p3 + ++p2
print("p5:",p5)//p5: Point.x = 31  Point.y = 31

p5 = p3 + p4++
print("p5:",p5)//p5: Point.x = 30  Point.y = 30

3. Equatable协议(比较两个实例是否等价)

class Person: Equatable {
    var age: Int
    init(age: Int) {
        self.age = age
    }
    public static func == (lhs: Person, rhs: Person) -> Bool {
       
        return  lhs.age == rhs.age
    }
}

var p1 = Person(age: 10)
var p2 = Person(age: 10)

print(p1 == p2)//true
print(p1 != p2)//false

注意
  如果Person类不继承Equatable协议,只能使用==运算符,!=运算符不可用;
  如果Person类继承了Equatable协议,如果重载了 ==运算符,也就等价于重载了 !=运算符

  1. 没有关联类型的枚举类型
  2. 只拥有遵守Equatable协议关联类型的枚举
  3. 只拥有遵守Equatable协议存储属性的结构体
enum Answer: Equatable {
    case wrong(Int)
    case right
}
var s1 = Answer.wrong(10)
var s2 = Answer.wrong(10)
print(s1 == s2)

4. Comparable (比较大小)

  要想比较2个实例大小,一般做法是:遵守Comparable协议
、重载相应的运算符

// 规则:score大的比较大,若score相等,age小的比较大
struct Student:Comparable {
    var score: Int
    var age: Int
    init(score: Int, age: Int) {
        self.score = score
        self.age = age
    }
    
    
    static func < (lhs: Self, rhs: Self) -> Bool{
        (lhs.score < rhs.score) || (lhs.score == rhs.score && lhs.age > rhs.age)
    }
    static func <= (lhs: Self, rhs: Self) -> Bool{
        (lhs.score < rhs.score) || (lhs.score == rhs.score && lhs.age == rhs.age)

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

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

}

var s1 = Student(score: 88, age: 18)
var s2 = Student(score: 88, age: 20)
var s3 = Student(score: 90, age: 18)
var s4 = Student(score: 70, age: 10)

print(s1 > s2)   //true
print(s2 > s3)   //false
print(s3 >= s4)   //true
print(s4 <= s4)   //true

5. 自定义运算符(Operator)了解

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

prefix operator +++

prefix func +++ (_ i: inout Int){
    i += 2
}

var age = 10
+++age

print(age)//12

6. Extension(扩展)

可以添加方法、计算属性、下标、(便捷)初始化器、嵌套类型、协议等等

  1. 不能覆盖原有功能
  2. 不能添加存储属性,不能向已有的属性添加属性观察器
  3. 不能添加父类
  4. 不能添加指定初始化器,不能添加反初始化器
  5. ......

注意:指定、便捷初始化器是类里面的概念

如果希望自定义初始化器的同时,编译器也能够生成默认初始化器,则可以在扩展中编写初始化器。

required初始化器也不能写在扩展中

extension Array {
    subscript(nullable idx: Int) ->Element?{
        if(startIndex..<endIndex).contains(idx){
            return self[idx]
        }
        return nil
    }
}
var arr:Array<Int> = [11,22,33,44]
print(arr[nullable: -1] as Any)//nil
print(arr[nullable: 1] as Any)//22

给一个类扩展协议

class Person {
    var age = 0
    init(age: Int) {
        self.age = age
    }
    
}
extension Person: Equatable{
    static func == (lhs: Person, rhs: Person) -> Bool{
        if lhs.age == rhs.age {
           return true
        }
        return false
    }

}

var p1 = Person(age: 10)
var p2 = Person(age: 10)
print(p1 == p2)//true

扩展协议:
扩展可以给协议提供默认实现,也可以间接实现【可选协议】的效果
扩展可以给协议扩充【协议从未声明过的方法】

//是否是奇数
extension BinaryInteger{
    func isOdd() -> Bool {
        self % 2 != 0
    }
}

print(10.isOdd())//false
print(3.isOdd())//true
上一篇下一篇

猜你喜欢

热点阅读