Welcome to Swift (蘋果官方Swift文檔初譯與註解二十七)---189~198頁(第四章-- 流程式控制制)

來源:互聯網
上載者:User

標籤:style   使用   strong   代碼   io   res   

Switch

  一個switch語句裡包含一個值,並且用這個值與其他幾個可能的匹配模式進行比較,然後根據成功匹配上的模式,執行相應的代碼塊.switch語句提供了比if語句更多的選項來相應多種潛  在的情況.

  最簡單的一個例子:

    switch some value to consider {

    case value 1:

         respond to value 1

    case value 2,

    value 3:

        respond to value 2 or 3

    default:

        otherwise, do something else

    }

  每一個switch語句都包含多個case關鍵字.另外為了比較指定的值,Swift針對每個case提供了幾種不同的匹配模式,這些將在後面的部分進行說明.

  每個switch的case體都是一個獨立的代碼執行分支,這與if語句的分支有點類似.switch語句決定哪一個分支會被選中執行.

  switch語句必須是周全的,也就是每種可能的值都會匹配到一個case.如果不是這樣,你可以通過關鍵字default來定義一個預設的case,它預設總是出現在最後面.

  下面的例子使用了switch語句來處理一個小寫字元(someCharacter):

    let someCharacter: Character = "e"

    switch someCharacter {

    case "a", "e", "i", "o", "u":

        println("\(someCharacter) is a vowel")

    case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",

    "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":

         println("\(someCharacter) is a consonant")

    default:

        println("\(someCharacter) is not a vowel or a consonant")

    }

    // prints "e is a vowel”

  這個switch語句的第一個case匹配了英語裡所有的五個母音,類似的,第二個case匹配了英語中其他小寫字母.

  在一個switch的case中寫盡其他所有的可能性是不實際的,因此switch語句提供了一個default的case用來匹配其他情況,這樣保證switch語句盡量窮盡.

No Implicit Fall through (非預設降落執行)

  與C語言和OC中的switch語句比較,在Swift中switch語句不會在每個case語句後預設的掉落到後面下一個case,相反的,整個switch語句在第一個匹配的case執行完成之後就結束了.  這使得switch比C語言的更加安全和清晰,避免了多個case的執行錯誤.

  注意點:

  你仍可以在一個匹配的case中,在這個case執行完成之前,提前跳出,如果你需要這樣的話.

  每個case比較包含至少一個可以執行的語句,下面代碼的寫法是錯誤的,因為第一個case是空的:

    let anotherCharacter: Character = "a"

    switch anotherCharacter {

    case "a":

    case "A":

        println("The letter A")

    default:

        println("Not the letter A")

    }

    // this will report a compile-time error

  不像C語言的switch語句那樣不匹配"a" 和 "A",相反編譯器會報告錯誤,case “a”:沒有包含任何執行語句.這個方法避免了從一個case掉落的另一個case,使得代碼更加安全和幹     淨.

  在一個switch的case中有多個匹配值可以使用逗號(,)分隔,也可以分成多行來寫:

    switch some value to consider {

    case value 1,

    value 2:

         statements

    }

  注意點:

  為了使一個特定的switch可以有掉落的功能,可以使用關鍵字fallthrough.

Range Matching (範圍匹配)

  在switch的case中可以檢查值是否包含在一個範圍.下面的例子使用數字範圍來提供了自然語言的關於任意大小數位情況:

    let count = 3_000_000_000_000

    let countedThings = "stars in the Milky Way"

    var naturalCount: String

    switch count {

    case 0:

        naturalCount = "no"

    case 1...3:

        naturalCount = "a few"

    case 4...9:

        naturalCount = "several"

    case 10...99:

         naturalCount = "tens of"

    case 100...999:

        naturalCount = "hundreds of"

    case 1000...999_999:

        naturalCount = "thousands of"

    default:

        naturalCount = "millions and millions of"

    }

    println("There are \(naturalCount) \(countedThings).")

    // prints "There are millions and millions of stars in the Milky Way.”

Tuples (元組)

  可以在同一個switch語句中使用元組來測試多個值.元組中的每一個元素都可以用來測試不同的值或者範圍.也可以使用底線(_)標識出任意可能的匹配值.

  下面的例子中,使用與點(x,y)使用一個簡單的(Int,Int)類型元組來表示,並它在圖形上的位置:

    let somePoint = (1, 1)

    switch somePoint {

    case (0, 0):

        println("(0, 0) is at the origin")

    case (_, 0):

        println("(\(somePoint.0), 0) is on the x-axis")

    case (0, _):

        println("(0, \(somePoint.1)) is on the y-axis")

    case (-2...2, -2...2):

        println("(\(somePoint.0), \(somePoint.1)) is inside the box")

    default:

        println("(\(somePoint.0), \(somePoint.1)) is outside of the box")

    }

    // prints "(1, 1) is inside the box”

    “let somePoint = (1, 1)

    switch somePoint {

    case (0, 0):

        println("(0, 0) is at the origin")

    case (_, 0):

        println("(\(somePoint.0), 0) is on the x-axis")

    case (0, _):

        println("(0, \(somePoint.1)) is on the y-axis")

    case (-2...2, -2...2):

        println("(\(somePoint.0), \(somePoint.1)) is inside the box")

    default:

        println("(\(somePoint.0), \(somePoint.1)) is outside of the box")

    }

    // prints "(1, 1) is inside the box”

    

  例子中,switch語句判斷這個點是否在原點(0,0),是否在紅色的X軸,是否在橘色的Y軸;是否在藍色的4*4的盒子內,或者在盒子外.

  不像C語言中的switch允許多個case可以處理同一個值.實際上,上面例子中原點(0,0)可以匹配所有的四種情況,然而如果多種匹配,那麼第一個匹配的case總是被選中,因此點(0,0)將  會匹配到case (0, 0),因此所有其他的匹配都會被忽略.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.