Swift - 模式

作者: aven_kang | 来源:发表于2022-04-25 00:28 被阅读0次

    通配符模式

    _ 匹配任何值
    _? 匹配非nil值
    enum Lift {
        
        case human(name:String,age:Int?)
        case animal(name:String,age:Int?)
         
    }
    
    func check(_ life:Lift) {
        
        switch life {
        case .human(let name, _):
            print(name)
        case .animal(let name, _?):
            print(name)
        default:
            print("hahahah")
        }
    }
    
    这里需要注意case .animal(let name, _?),这里意思是,必须是animal这个case,然后age是不能为空的,即使我不使用到它
    func check(_ life:Lift) {
        
        switch life {
        case .human(let name, _):
            print(name)
        case .animal(let name, _?):
            print(name)
        default:
            print("other")
        }
    }
    
    check(.human(name: "Rose", age: 20))
    check(.human(name: "Jack", age: nil))
    check(.animal(name: "Dog", age: 5))
    check(.animal(name: "Cat", age: nil))
    
    Rose
    Jack
    Dog
    other
    

    可以看出打印,最后一个是other,因为最后一个的age是nil,所以不符合

    case .animal(let name, _?):
    

    上一行代码中,可以看出,_?是非空

    示例
            var num:Int? = 10
            num = nil
            switch num {
            case let v?:
                print(v)
            case nil:
                print("nil")
            }
    

    在这里,程序是走到了print("nil")这里,因为num后面别赋值为nil,所以是走不到let v?这里的,如果num不为空,按照程序,是走到打印print(v)这里的

    值绑定模式

    let Point = (3,2)
    switch Point {
      case let (x,y):
      print("The point is at (\(x),\(y))")
    }
    

    元组模式

    let points = [(0,0),(1,0),(2.0)]
    for (x,_) in points {
        print(x)
    }
    
    let name:String? = "Jack"
    let age = 18
    let info:Any = [1,2]
    switch (name,age,info) {
        case (_?,_,_as String):
            print("case")
        default:
            print("default")
    }
    
    var scores = ["jack":98,"rose":100,"kate":86]
    for (name,score) in scores {
          print(name,score)
    }
    

    枚举Case模式

    if case语句等价于只有1个case的switch语句
    let age = 2
     if age >= 0 && age <= 9 {
           print("[0,9]")
     }
    

    从上面的代码可以看出,是一个简单的判断语句,0到9之间的判断
    我们还可以这么写

    if case 0...9 = age {
          print("[0,9]")
     }
    

    上面这段代码的写法,实际上等价于下面的代码

    switch age {
        case 0...9:
            print("[0,9]")
        default:
            print("other")
     }
    

    同理,guard也是可以这么用的

    guard case 0...9 = age else { return }
    print("[0,9]")
    
    for case nil in aegs {
          print("有nil值")
          break
    }
    
    let points = [(1,0),(2,2),(3,0)]
    for case let (x,0) in points {
         print(x)
    }  // 1,3
    

    可选模式

    截屏2022-03-05 下午1.53.39.png

    类型转换模式

    截屏2022-03-05 下午2.02.38.png

    表达式模式

    let point = (1,2)
         switch point {
         case (0,0):
            print("(0,0) is at the origin")
         case (-2...2,-2...2):
            print("(\(point.0),\(point.1) is near the origin")
         default:
            print("other")
    }
    

    自定义表达式模式

    struct Student {
        
        var score:Int = 0, name = ""
        
        static func ~= (pattern:Int,value:Student) -> Bool {
            value.score >= pattern
        }
        
        static func ~= (pattern:Range<Int>,value:Student) -> Bool {
            pattern.contains(value.score)
        }
        
        static func ~= (pattern:ClosedRange<Int>,value:Student) -> Bool {
            pattern.contains(value.score)
        }
        
        
    }
    
    var stu = Student(score: 10, name: "jack")
            switch stu {
            case 100:
                print(">=100")
            case 90:
                print(">=90")
            case 80..<90:
                print("[80,90]")
            case 60...79:
                print("[60,79]")
            case 0:
                print(">=0")
            default:break
            }
    
    if case 60 = stu. // 这么写也是可以的
    

    相关文章

      网友评论

        本文标题:Swift - 模式

        本文链接:https://www.haomeiwen.com/subject/hismrrtx.html