SWIFT學習筆記05

來源:互聯網
上載者:User

標籤:swift   筆記   fallthrough   case   break   

1、Swift 無需寫break,所以不會發生這種貫穿(fallthrough)的情況。2、//用不到變數名,可用“_”替換
for _ in 1...power{    answer *= base}

3、case 可以匹配更多的類型模式,包括區間匹配(range matching),元組(tuple)和特定類型的描述。
可以這樣用case
case 1...3:naturalCount = "a few"

4、如果存在多個匹配,那麼只會執行第一個被匹配到的 case 分支,剩下的能夠匹配的 case 分支都會被忽視掉。

5、case值綁定,此例子都沒必要用default
let anotherPoint = (2, 0)switch anotherPoint {case (let x, 0):println("on the x-axis with an x value of \(x)")case (0, let y):println("on the y-axis with a y value of \(y)")case let (x, y):println("somewhere else at (\(x), \(y))") } // 輸出 "on the x-axis with an x value of 2"

6、//若且唯若where語句的條件為true時,匹配到的 case 分支才會被執行。
let yetAnotherPoint = (1, -1)switch yetAnotherPoint{case let (x, y)where x == y:println("(\(x), \(y)) is on the line x == y")case let (x, y) where x == -y:println("(\(x), \(y)) is on the line x == -y")case let (x, y):println("(\(x), \(y)) is just some arbitrary point")} // 輸出 "(1, -1) is on the line x == -y"

2014年07月03日



相關文章

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.