Skip to content

if_switch_expressions

ShenYj edited this page Oct 23, 2023 · 1 revision

if and switch expressions

if、switch 语法进一步精简

  • 原来

    func test(_ flag: Bool) {
            
        let string: Stirng
        if flag {
            string = "true"
        }
        else {
            string = "false"
        }
        print(string)
    }
    func test(_ flag: Bool) {
            
        let string: Stirng
        switch flag {
            case true:  string = "true"
            case false: string = "false"
        }
        print(string)
    }
  • 现在在 Swift 5.9 下

    func test(_ flag: Bool) {
            
        let string = if flag { "true" } else { "false" }
        
        print(string)
    }
    func test(_ flag: Bool) {
            
        let string = switch flag {
            case true:  "true"
            case false: "false"
        }
        
        print(string)
    }

Getting Started

Social

Clone this wiki locally