版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.07.26 |
前言
我是swift2.0的時(shí)候開(kāi)始接觸的,記得那時(shí)候還不是很穩(wěn)定,公司的項(xiàng)目也都是用oc做的,并不對(duì)swift很重視,我自己學(xué)了一段時(shí)間,到現(xiàn)在swift3.0+已經(jīng)出來(lái)了,自己平時(shí)也不寫(xiě),忘記的也差不多了,正好項(xiàng)目這段時(shí)間已經(jīng)上線了,不是很忙,我就可以每天總結(jié)一點(diǎn)了,希望對(duì)自己對(duì)大家有所幫助。在總結(jié)的時(shí)候我會(huì)對(duì)比oc進(jìn)行說(shuō)明,有代碼的我會(huì)給出相關(guān)比對(duì)代碼。
1. swift簡(jiǎn)單總結(jié)(一)—— 數(shù)據(jù)簡(jiǎn)單值和類型轉(zhuǎn)換
2. swift簡(jiǎn)單總結(jié)(二)—— 簡(jiǎn)單值和控制流
3. swift簡(jiǎn)單總結(jié)(三)—— 循環(huán)控制和函數(shù)
4. swift簡(jiǎn)單總結(jié)(四)—— 函數(shù)和類
5. swift簡(jiǎn)單總結(jié)(五)—— 枚舉和結(jié)構(gòu)體
6. swift簡(jiǎn)單總結(jié)(六)—— 協(xié)議擴(kuò)展與泛型
7. swift簡(jiǎn)單總結(jié)(七)—— 數(shù)據(jù)類型
8. swift簡(jiǎn)單總結(jié)(八)—— 別名、布爾值與元組
9. swift簡(jiǎn)單總結(jié)(九)—— 可選值和斷言
10. swift簡(jiǎn)單總結(jié)(十)—— 運(yùn)算符
11. swift簡(jiǎn)單總結(jié)(十一)—— 字符串和字符
12. swift簡(jiǎn)單總結(jié)(十二)—— 集合類型之?dāng)?shù)組
13. swift簡(jiǎn)單總結(jié)(十三)—— 集合類型之字典
控制流
控制流包括if
、while
等循環(huán)邏輯處理,可以說(shuō)是邏輯處理中很重要的一部分,同樣在oc
中也起到很大的作用。這里需要說(shuō)明的是swift
的case
語(yǔ)句很強(qiáng)大,在oc
中case語(yǔ)句如果不寫(xiě)break
會(huì)發(fā)生fallThrough
貫穿情況,但是在swift
中不一樣,不用寫(xiě)break不會(huì)發(fā)生貫穿現(xiàn)象,case
還可以匹配更多的類型模式,包括區(qū)間匹配(range matching)、元組(tuple)和特定類型的描述,switch
的case語(yǔ)句中匹配的值可以是由case體內(nèi)部臨時(shí)的變量或者常量決定,也可以由where
分句描述更復(fù)雜的匹配條件。
For 循環(huán)
For
循環(huán)有兩種,前兩講到數(shù)組和字典的時(shí)候,其實(shí)都已經(jīng)接觸過(guò)了。
-
for - in
實(shí)現(xiàn) -
for
條件遞增(for - condition - increment
)語(yǔ)句實(shí)現(xiàn)
1. for - in 循環(huán)實(shí)現(xiàn)
這里就只給出一個(gè)簡(jiǎn)單例子。
var answer = 1
let base = 3
let power = 5
for number in 1...power {
answer *= base
}
print(answer)
下面看輸出結(jié)果
243
這里大家會(huì)發(fā)現(xiàn)我們?cè)?code>for - in循環(huán)中,number
那個(gè)臨時(shí)變量我們并沒(méi)有用到,像這種情況我們可以用_
代替。所以代碼變成了下面的樣子。
var answer = 1
let base = 3
let power = 5
for _ in 1...power {
answer *= base
}
print(answer)
結(jié)果沒(méi)有改變,這里就不給出了,oc
的代碼也不給出了,前面都給出過(guò)了。
2. For條件遞增(for - condition - increment
)
這個(gè)在C
和OC
中都有的語(yǔ)法也不多說(shuō)了,而在swift
3.0這種遞增的循環(huán)C風(fēng)格的代碼已經(jīng)被取消了,替代的方案就是上面的那種for - in
循環(huán)。
var answer = 1
let base = 3
let power = 5
for index = 1; index < power; index = index + 1 {
answer *= base
}
注意:上面的C
風(fēng)格的循環(huán)已經(jīng)被取消了。
While循環(huán)
while
循環(huán)是只有條件變?yōu)?code>false,循環(huán)才會(huì)終止。swift
有兩種while
循環(huán)。
-
while
循環(huán),每次循環(huán)開(kāi)始時(shí)計(jì)算條件是否符合 -
do - while
循環(huán),每次在循環(huán)結(jié)束的時(shí)候計(jì)算條件是否符合。
1. while循環(huán)
while
循環(huán)的格式
while condition {
statements
}
下面給出一個(gè)簡(jiǎn)單的swift
中的例子,OC
中很簡(jiǎn)單不在給出。
var answer = 1
let base = 3
var power = 5
while power > 0 {
answer *= base
power -= 1
}
print(answer)
下面看輸出結(jié)果
243
2. do - while 循環(huán)
它和while
的區(qū)別是在判斷循環(huán)條件之前,先執(zhí)行一次循環(huán)的代碼塊,然后判斷循環(huán)條件。具有下面的格式。這里還要注意swift 3.0
以后,do - while
變成了repeat - while
了。
do {
statements
} while condition
下面還是看一個(gè)簡(jiǎn)單例子,同時(shí)也不給出OC
的例子了。
var answer = 1
let base = 3
var power = 5
repeat {
answer *= base
power -= 1
} while power > 5
print(answer)
下面看輸出結(jié)果
3
條件語(yǔ)句
條件語(yǔ)句,swift
提供了兩種類型,分別為if
語(yǔ)句和switch
語(yǔ)句。條件比較少的時(shí)候用if
語(yǔ)句,條件比較多的時(shí)候就用switch
語(yǔ)句。
1. if語(yǔ)句
下面給出一個(gè)swift
的一個(gè)簡(jiǎn)單例子。
let temperature = 40
if temperature >= 100 {
print("沸騰了")
}
else if temperature > 50 && temperature < 100 {
print("半熟了")
}
else if temperature > 30 && temperature < 50 {
print("很熱了")
}
else {
print("涼爽了")
}
下面看輸出結(jié)果
很熱了
2. switch語(yǔ)句
switch
語(yǔ)句沒(méi)有貫穿現(xiàn)象,所以也就是說(shuō)switch
語(yǔ)句必須是完備的,每一個(gè)可能的值必須至少有一個(gè)case
分支與之對(duì)應(yīng),如果有不可能覆蓋的情況,就用default
分支滿足要求,默認(rèn)分支要寫(xiě)在switch
語(yǔ)句之后。
看下面這個(gè)swift
例子,OC
情況仍然不給出。
let someCharacter : Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(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":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a consonant or vowel")
}
下面看輸出結(jié)果
e is a vowel
switch不存在隱式貫穿(No Implicit FallThrough)
swift
中,當(dāng)匹配的case
分支中的代碼執(zhí)行完畢后,程序會(huì)終止switch
語(yǔ)句,而不會(huì)繼續(xù)執(zhí)行下一個(gè)case
分支。并且case
下面的不能是空的,不會(huì)發(fā)生貫穿,像下面這么寫(xiě)是會(huì)報(bào)錯(cuò)的。
let someCharacter : Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a consonant or vowel")
}
會(huì)報(bào)下面的錯(cuò)誤
'case' label in a 'switch' should have at least one executable statement
一個(gè)case
也可以包含多個(gè)模式,用逗號(hào)將他們分開(kāi),具體格式如下。
switch some value to consider {
case value 1,
value 2,
statements
}
如果你想貫穿到其他的case
分支中,請(qǐng)使用fallThrough
語(yǔ)句,這個(gè)后面會(huì)和大家講。
區(qū)間匹配 (Range Matching)
case
分支的模式也可以是一個(gè)值的區(qū)間,具體看下面的例子。
let count = 3_000_000_000_000
var starCount : String
switch count {
case 0:
starCount = "no"
case 1...10:
starCount = "a few"
case 11...999:
starCount = "hundreds of"
case 999...999_999:
starCount = "thousands of"
default:
starCount = "millons and millons of"
}
print("There are \(starCount) stars in the sky")
下面看輸出結(jié)果
There are millons and millons of stars in the sky
元組 (Tuple)
你可以使用元組在同一個(gè)switch
語(yǔ)句中測(cè)試多個(gè)值,元組中的元素可以是值,也可以是區(qū)間,另外,使用下劃線_
來(lái)匹配所有可能的情況。
下面我們看一個(gè)swift
中的例子。
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("(0, 0) is at origin")
case (_, 0):
print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
print("(0,\(somePoint.1) is on the y-axis")
case (-2...2, -2...2):
print("((\(somePoint.0),\(somePoint.1)) inside the box")
default:
print("((\(somePoint.0),\(somePoint.1)) is outside the box")
}
下面看輸出結(jié)果
((1,1) inside the box
值綁定 (Value Bindings)
case
分支的模式允許將匹配的值綁定到一個(gè)臨時(shí)的常量或者變量,這些常量或者變量在該case
分支里就可以被引用了,這種行為被稱為值綁定(Value Bindings)。
下面看一下例子。
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with an y value of \(y)")
case let (x, y):
print("somewhere at (\(x), \(y))")
}
下面看輸出結(jié)果
on the x-axis with an x value of 2
這里沒(méi)有默認(rèn)default
分支,因?yàn)樗械?code>case已經(jīng)覆蓋了所有的可能性。
where
case
分支模式可以使用where
語(yǔ)句來(lái)判斷額外的條件。
下面看一個(gè)簡(jiǎn)單的例子。
let anotherPoint = (1, -1)
switch anotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y))is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y))is on the line x == -y")
case let (x, y):
print("(\(x), \(y))is just some arbitrary point")
}
下面看輸出結(jié)果
(1, -1)is on the line x == -y
后記
未完,待續(xù)~~~