本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆,基本的語法不作介紹楼镐,主要介紹Swift中的一些特性或者與OC差異點(diǎn)。
系列文章:
While
Repeat-While
The repeat-while loop in Swift is analogous to a do-while
loop in other languages.
- Swift中
repeat-while
的邏輯類似其他語言中do-while
绵患,也是搞不懂Swift為什么要自己寫一套句灌。
例子:
repeat{
print("invoked");
}while(false);
執(zhí)行結(jié)果:
invoked
Switch
In its simplest form, a switch statement compares a value
against one or more values of the same type.
- Switch中的一個(gè)case語句可以寫入一個(gè)或者多個(gè)同類型的值。
例子:
func distinguishNumber(number: Int){
switch number{
case 2, 4, 6:
print("\(number) is even");
case 1, 3, 5:
print("\(number) is odd");
default:
print("\(number) is exceed");
}
}
distinguishNumber(number: 2);
distinguishNumber(number: 3);
distinguishNumber(number: 8);
執(zhí)行結(jié)果:
2 is even
3 is odd
8 is exceed
不存在隱式穿透(No Implicit Fallthrough)
In contrast with switch statements in C and Objective-C,
switch statements in Swift do not fall through the bottom
of each case and into the next one by default. Instead,
the entire switch statement finishes its execution as soon
as the first matching switch case is completed, without
requiring an explicit break statement. This makes the
switch statement safer and easier to use than the one in C
and avoids executing more than one switch case by mistake.
- Swift與OC,C中關(guān)于Swich中存在差異料身,Swift中默認(rèn)匹配到執(zhí)行的case,執(zhí)行完成直接break汤纸,而其他的語言O(shè)C,C必須加入顯式的break才能跳出。
例子直接參考上述的例子即可芹血。
Interval Matching(間隔匹配)
Values in switch cases can be checked for their inclusion in an
interval
- Swich case中的匹配值可以是區(qū)間類型數(shù)據(jù)贮泞。
例子:
func distinguishNumber(number: Int){
switch number{
case 1...10:
print("\(number) <= 10");
case 10...Int.max:
print("\(number) > 10");
default:
print("\(number) is exceed");
}
}
distinguishNumber(number: 2);
distinguishNumber(number: 13);
執(zhí)行結(jié)果:
2 <= 10
13 > 10
Tuples
You can use tuples to test multiple values in the same
switch statement. Each element of the tuple can be tested
against a different value or interval of values.
Alternatively, use the underscore character (_), also
known as the wildcard pattern, to match any possible
value.
- Swich case中也可以使用元組楞慈,元組中的值也可以是區(qū)間類型數(shù)據(jù)。
例子:
func distinguishPostion(x: Int,y :Int){
let somePoint = (x, y)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
}
distinguishPostion(x: 0, y: 0);
distinguishPostion(x: 1, y: 1);
執(zhí)行結(jié)果:
(0, 0) is at the origin
(1, 1) is inside the box
Value Bindings
A switch case can name the value or values it matches to
temporary constants or variables, for use in the body of
the case. This behavior is known as value binding, because
the values are bound to temporary constants or variables
within the case’s body.
- Switch case中可以寫入臨時(shí)的變量綁定傳入的值啃擦。
例子:
func distinguishPostion(x: Int,y :Int){
let somePoint = (x, y)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (let tempX, 0):
print("\(somePoint) is on the x-axis,and x is \(tempX)")
case (0, let tempY):
print("\(somePoint) is on the y-axis,and y is \(tempY)")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
}
distinguishPostion(x: 1, y: 0);
distinguishPostion(x: 0, y: 1);
執(zhí)行結(jié)果:
(1, 0) is on the x-axis,and x is 1
(0, 1) is on the y-axis,and y is 1
Where
A switch case can use a where clause to check for additional
conditions.
- Switch case中可以使用where 語句去做額外的條件判斷囊蓝。
例子:
func distinguishPostion(x: Int,y :Int){
let somePoint = (x, y)
switch somePoint {
case let (x, y) where x == y:
print("\(somePoint) is on the line x == y")
case let (x, y) where x == -y:
print("\(somePoint) is on the line x == -y")
case let (x, y):
print("\(somePoint) is just some arbitrary point")
}
}
distinguishPostion(x: 0, y: 0);
distinguishPostion(x: 1, y: -1);
distinguishPostion(x: 0, y: 1);
執(zhí)行結(jié)果:
(0, 0) is on the line x == y
(1, -1) is on the line x == -y
(0, 1) is just some arbitrary point
Control Transfer Statements(控制轉(zhuǎn)移狀態(tài))
Control transfer statements change the order in which your
code is executed, by transferring control from one piece
of code to another. Swift has five control transfer
statements:
continue
break
fallthrough
return
throw
- Swift中存在5種控制轉(zhuǎn)移狀態(tài),即以上的5種令蛉。
我們重點(diǎn)了解fallthrough
,throw
的使用聚霜。
Fallthrough
In Swift, switch statements don’t fall through the bottom
of each case and into the next one. That is, the entire
switch statement completes its execution as soon as the
first matching case is completed. By contrast, C requires
you to insert an explicit break statement at the end of
every switch case to prevent fallthrough. Avoiding default
fallthrough means that Swift switch statements are much
more concise and predictable than their counterparts in C,
and thus they avoid executing multiple switch cases by
mistake.
If you need C-style fallthrough behavior, you can opt in
to this behavior on a case-by-case basis with the
fallthrough keyword. The example below uses fallthrough to
create a textual description of a number.
- Switch case如果需要能當(dāng)首個(gè)case執(zhí)行完能執(zhí)行到第二個(gè)case中必須加入
Fallthrough
關(guān)鍵字。
例子:
func distinguishPostion(x: Int,y :Int){
let somePoint = (x, y)
switch somePoint {
case let (x, y) where x == y:
print("\(somePoint) is on the line x == y")
fallthrough;
case let (_, 1):
print("\(somePoint) is on the line y == 1")
case let (x, y):
print("\(somePoint) is just some arbitrary point")
}
}
distinguishPostion(x: 1, y: 1);
distinguishPostion(x: 1, y: -1);
distinguishPostion(x: 0, y: 1);
執(zhí)行結(jié)果:
(1, 1) is on the line x == y
(1, 1) is on the line y == 1
(1, -1) is just some arbitrary point
(0, 1) is on the line y == 1
The fallthrough keyword does not check the case conditions
for the switch case that it causes execution to fall into.
The fallthrough keyword simply causes code execution to
move directly to the statements inside the next case (or
default case) block, as in C’s standard switch statement
behavior.
-
fallthrough
的關(guān)鍵字不會(huì)去檢查case中的條件判斷珠叔,寫了條件判斷也無效蝎宇。
例子:上述例子我們執(zhí)行distinguishPostion(x: 1, y: 2);
結(jié)果不變。
標(biāo)記語言(Labeled Statements)
In Swift, you can nest loops and conditional statements
inside other loops and conditional statements to create
complex control flow structures. However, loops and
conditional statements can both use the break statement to
end their execution prematurely. Therefore, it is
sometimes useful to be explicit about which loop or
conditional statement you want a break statement to
terminate. Similarly, if you have multiple nested loops,
it can be useful to be explicit about which loop the
continue statement should affect.
To achieve these aims, you can mark a loop statement or
conditional statement with a statement label. With a
conditional statement, you can use a statement label with
the break statement to end the execution of the labeled
statement. With a loop statement, you can use a statement
label with the break or continue statement to end or
continue the execution of the labeled statement.
- Swift 中標(biāo)記語言這里說的很明白祷安,就是多層循環(huán)中可以利用標(biāo)記結(jié)合
break
,continue
達(dá)到很方便結(jié)束標(biāo)記的某個(gè)循環(huán)邏輯姥芥。
例子:
let xLimitNum :Int = 20;
let yLimitNum :Int = 6;
var xSum :Int = 0;
var ySum :Int = 0;
xLoop:for x in 1...4{
ySum = 0;
yLoop:for y in 1...4{
if ySum + y > yLimitNum{
print("ySum:\(xSum) + y:\(y) > \(yLimitNum) break yLoop");
break yLoop;
}
if xSum + y > xLimitNum{
print("xSum:\(xSum) + y:\(y) > \(xLimitNum) break xLoop");
break xLoop;
}
ySum += y;
xSum += y;
print("xSum:\(xSum - y) + y:\(y) = \(xSum)");
print("ySum:\(ySum - y) + y:\(y) = \(ySum)");
}
}
執(zhí)行結(jié)果:
xSum:0 + y:1 = 1
ySum:0 + y:1 = 1
xSum:1 + y:2 = 3
ySum:1 + y:2 = 3
xSum:3 + y:3 = 6
ySum:3 + y:3 = 6
ySum:6 + y:4 > 6 break yLoop
xSum:6 + y:1 = 7
ySum:0 + y:1 = 1
xSum:7 + y:2 = 9
ySum:1 + y:2 = 3
xSum:9 + y:3 = 12
ySum:3 + y:3 = 6
ySum:12 + y:4 > 6 break yLoop
xSum:12 + y:1 = 13
ySum:0 + y:1 = 1
xSum:13 + y:2 = 15
ySum:1 + y:2 = 3
xSum:15 + y:3 = 18
ySum:3 + y:3 = 6
ySum:18 + y:4 > 6 break yLoop
xSum:18 + y:1 = 19
ySum:0 + y:1 = 1
xSum:19 + y:2 > 20 break xLoop
注意標(biāo)記:xLoop,yLoop