zoukankan      html  css  js  c++  java
  • Swift中的常用匹配方式

    在swift中,除了平时oc中所用的匹配时我们还可以这样使用匹配方式

    let pt1 = (x:0.5, y:1)
            
            switch pt1 {
            case (0, 0):
                print("在原点")
            case (0, _):
                print("在x轴")
            case (_, 0):
                print("在y轴")
            case (-1...1, -1...1):
                print("值在-1到1之间")
            case (_, _):
                print("不在x轴或y轴,也不在-1~1之间")
            }
    

    匹配时进行值绑定

            let pt1 = (x:0.5, y:0)
            
            switch pt1 {
            case (let x, 0):
                print("((x), 0) is on x axis")
            case (0, let y):
                print("(0, (y)) is on y axis")
            default:
                break
            }     
    

    自动提取optional的值进行判断:

            let skills: [String?] =
                ["Swift", nil, "PHP", "JavaScirpt", nil]
            
            for case let skill? in skills {
                print(skill) // Swift PHP JavaScript
            }
    

    值类型判定:

            let someValues: [Any] = [1, 1.0, "One"]
            
            for value in someValues {
                switch value {
                case let v as Int:
                    print("Integer (v)")
                case let v as Double:
                    print("Double (v)")
                case let v as String:
                    print("String (v)")
                default:
                    print("Invalid value")
                }
            }
    

    除了使用具体的数值对循环或分支条件约束外,我们还可以使用where进行更复杂的约束, eg:

            for i in 1...10 where i % 2 == 0 {
                print(i)
            }
    

    在实际工作中我们也可以将case、where等多个匹配语句结合来使我们的代码更为简单, eg:假设我们定义下面这样的enum表示手机电量

    enum Power {
        case fullyCharged
        case normal(percentage: Double)
        case outOfPower
    }
    

    然后,定义一个变量battery表示手机电池:

    let battery = Power.normal(percentage: 0.1)
    

    这样,我们就可以在绑定.normal associated value的同时,使用where进一步约束它的关联值了:

    switch battery {
    case .normal(let percentage) where percentage <= 0.1:
        print("Almost out of power") 
    case .normal(let percentage) where percentage >= 0.8:
        print("Almost fully charged")
    default:
        print("Normal battery status")
    }
    

    通过“,”进行多情况判断, eg:

            let pt1 = (x:0, y:1)
            
            switch pt1 {
            case (0, 0):
                print("在原点")
            case (0, _),(_, 0):
                print("在数轴上")
            case (_, _):
                print("不在x轴或y轴,也不在-1~1之间")
            }
    

    同上面的一样,多值判断我们还可以这么使用,可以大大减少代码量, eg:

            let username = "11@boxue.io"
            let password = 11111111
    
            if case ("11@boxue.io", 11111111) = (username, password) {
                print("correct")
            }
    

    相对于oc中的多个值分别判断还是方便、简洁了一些的

  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/GoodmorningMr/p/9628560.html
Copyright © 2011-2022 走看看