1. 程式人生 > >SWIFT學習筆記05

SWIFT學習筆記05

print 類型 class log else 2014年 with sni swe

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日



SWIFT學習筆記05