• <small id="w8oog"></small>
  • <td id="w8oog"></td>
  • <small id="w8oog"></small><xmp id="w8oog"><td id="w8oog"></td><td id="w8oog"><li id="w8oog"></li></td><xmp id="w8oog"><td id="w8oog"></td>
  • <td id="w8oog"></td><td id="w8oog"><li id="w8oog"></li></td><small id="w8oog"></small>
  • <td id="w8oog"></td>
  • <small id="w8oog"></small>
  • <small id="w8oog"></small><td id="w8oog"></td>
    免費開源的iOS開發學習平臺

    Swift: 6.2 語句和流程控制--分支條件語句

    條件語句在程序語言設計中是必不可少的。Swift語言中提供了兩種類型的條件語句:if語句和switch語句。當條件較為簡單且可能的條件判斷情況較少時,會使用if語句。switch語句則更適用在條件較為復雜、可能情況較多且需要用到模式匹配(pattern-matching)的情境。

    #if語句
    Swift中if語句的語法跟Objective-C是一樣的,只不過在寫法上,if語句的條件判斷表達式可以省去左右括號。

    let five = 5
    if five < 5 {
        print("小于5")
    }else if five > 5{
        print("大于5")
    }else{
        print("等于5")
    }
    //打印結果:等于5
    

    #switch語句

    • 在Swift語言中,switch語句最簡單的形式就是把某個值與一個或若干個相同類型的值作比較。
    let someCharacter: Character = "z"
    switch someCharacter {
    case "a":
        print("26個字母的首字母")
    case "z":
        print("26個字母的尾字母")
    default:
        print("其他字符")
    }
    //打印結果 "26個字母的尾字母"
    
    • 與C語言和Objective-C中的switch語句不同,在Swift中,不存在隱式的貫穿(No Implicit Fallthrough),即當匹配的 case 分支中的代碼執行完畢后,程序會終止switch語句,而不會繼續執行下一個case分支。這也就是說,不需要在case分支中顯式地使用break語句。這使得switch語句更安全、更易用,也避免了因忘記寫break語句而產生的錯誤。這就要求在case語句中,至少要包含一條可執行語句。
    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a": // 這里會報錯,因為這個case語句沒有執行體
    case "A":
        print("字符A")
    default:
        print("不是字符A")
    }
    
    • 當然switch語句也是可以同時匹配多個條件的,只要在case語句后面的多個條件用逗號進行分隔即可。
    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "A", "a":
        print("字符A")
    default:
        print("不是字符A")
    }
    //打印結果:字符A    
    
    • case分支的模式也可以是一個值的區間。例如,在下面的例子中,展示了使用區間匹配來輸出任意數字對應的中文表達。
    let approximateCount = 51
    var chineseStr: String
    switch approximateCount {
    case 0:
        chineseStr = "零"
    case 1..<10:
        chineseStr = "個"
    case 10..<100:
        chineseStr = "十"
    case 100..<1000:
        chineseStr = "百"
    default:
        chineseStr = "未知"
    }
    print("\(approximateCount)最高位是\(chineseStr)位")
    //打印結果:51最高位是十位
    
    • switch語句可以用元組(x,y,z,...)來匹配多個值。元組中的每個值可以與特定的值或者值的區間進行匹配。此外,還可以使用下劃線(_)作為通配符表示與任何值都可以匹配。下面示例展示一個確定(x,y)坐標點在二維坐標系的哪條直線上面。
    let pos = (-1,1)
    switch pos {
    case (_,0):
        print("(\(pos.0),\(pos.1))在X軸上")
    case (0,_):
        print("(\(pos.0),\(pos.1))在Y軸上")
    case (-2...2,-2...2):
        print("(\(pos.0),\(pos.1))在矩形(-2,2),(-2,2)區域之內")
    default:
        print("(\(pos.0),\(pos.1))在矩形(-2,2),(-2,2)區域之外")
    }
    
    • switch的case分支可以把它匹配到的值綁定到臨時常量或者變量中以供執行體使用。我們把這種行為稱之為值綁定(value binding),不過臨時常量或變量的作用域是case的執行體。
    let anotherPoint = (0, 1)
    switch anotherPoint {
    case (let x, 0):
        print("在X軸上,且x的值是\(x)")
    case (0, let y):
        print("在Y軸上,且y的值是\(y)")
    case let (x, y):
        print("在其他地方 (\(x), \(y))")
    }
    // 打印結果:在Y軸上,且y的值是1
    
    • switch的case分支可以使用where語句來判斷額外的條件。如下是示例,可以用來判斷二維坐標是否坐落在x=y 或者 x=-y直線上面。
    let anotherPoint = (1, -1)
    switch anotherPoint {
    case (let x,let y) where x==y:
        print("(\(x),\(y))在直線x=y上")
    case (let x, let y) where x == -y:
        print("(\(x),\(y))在直線x=-y上")
    case let (x, y):
        print("在其他地方 (\(x), \(y))")
    }
    // 打印結果:(1,-1)在直線x=-y上
    

    示例代碼

    https://github.com/99ios/23.6.2

    ijzzijzzij亚洲大全|天天狠天天透天干天天|日本一本加勒比五月天伊人久久|久久久噜噜噜久久中文字幕色伊伊
  • <small id="w8oog"></small>
  • <td id="w8oog"></td>
  • <small id="w8oog"></small><xmp id="w8oog"><td id="w8oog"></td><td id="w8oog"><li id="w8oog"></li></td><xmp id="w8oog"><td id="w8oog"></td>
  • <td id="w8oog"></td><td id="w8oog"><li id="w8oog"></li></td><small id="w8oog"></small>
  • <td id="w8oog"></td>
  • <small id="w8oog"></small>
  • <small id="w8oog"></small><td id="w8oog"></td>