級(jí)別: ★☆☆☆☆
標(biāo)簽:「iOS」「Swift 5.1」「For-In」「標(biāo)簽語(yǔ)句」「Fallthrough」
作者: 沐靈洛
審校: QiShare團(tuán)隊(duì)
控制流
For-In循環(huán)
使用for-in
循環(huán)迭代數(shù)組
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
使用for-in
循環(huán)迭代字典
let airports = ["job": "將軍", "age": "16", "name": "zhangfei"]
for (key,value) in airports {
print("\(key)")
print("\(value)")
}
使用for-in
循環(huán)迭代數(shù)值范圍
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
使用for-in
循環(huán)迭代數(shù)值范圍之使用stride(from:to:by :)
函數(shù):返回從起始值到結(jié)束值(不包括)惑灵,跳過指定量的序列魁蒜。從開始值起序列中的每個(gè)連續(xù)值都會(huì)增加相同范圍的幅度蛾派,直到下一個(gè)值等于或超過結(jié)束值。(以指定間隔迭代特定數(shù)值時(shí)的序列)电媳。符合Strideable
協(xié)議的任何類型的值都可以使用此函數(shù),例如整數(shù)或浮點(diǎn)類型庆亡。
for item in stride(from: 0, to: 60, by: 5) {
print(item, separator: "", terminator: " ")//!< 0 5 10 15 20 25 30 35 40 45 50 55
}
for item in stride(from: 0, to: Double.pi * 2, by: .pi/2) {
print(item, separator: "", terminator: " ")//!<0.0 1.5707963267948966 3.141592653589793 4.71238898038469
}
使用for-in
循環(huán)迭代數(shù)值范圍之使用stride(from:through:by:)
函數(shù):返回從起始值到結(jié)束值(可能包括)匾乓,跳過指定量的序列。(以指定間隔迭代特定數(shù)值時(shí)的序列)與stride(from:to:by :)
函數(shù)區(qū)別于它可能會(huì)包括結(jié)束值又谋。
for item in stride(from: 0, through: 60, by: 5) {
print(item, separator: "", terminator: " ")//!< 0 5 10 15 20 25 30 35 40 45 50 55 60
}
for item in stride(from: 0, through: Double.pi * 2, by: .pi/2) {
print(item, separator: "", terminator: " ")//!<0.0 1.5707963267948966 3.141592653589793 4.71238898038469 6.283185307179586
}
While循環(huán)
Swift提供了兩種while
循環(huán):
- while:在每次循環(huán)開始時(shí)判斷條件拼缝。
- repeat-while:在每次循環(huán)結(jié)束時(shí)判斷條件。
While
while
循環(huán)開始時(shí)判斷條件是否成立彰亥,若true
立即執(zhí)行方法體咧七,直到條件變?yōu)?code>false時(shí)停止執(zhí)行。
let adc = 10
var apc = 0;
while apc < adc {
apc += 1 //!< 必須要有終止的條件 若 apc < adc 恒成立則程序陷入死循環(huán)
print("條件成立") //10
}
Repeat-While
repeat-while
循環(huán):while
循環(huán)的變體任斋,類似于其他語(yǔ)言中的do-while
继阻,先執(zhí)行單個(gè)循環(huán),再考慮循環(huán)條件废酷,若為true
繼續(xù)重復(fù)循環(huán)瘟檩,直到條件為false
停止執(zhí)行。
let adc = 10
var apc = 0;
repeat {
apc += 1
print("條件成立") //!< 10次
}while apc < adc
條件語(yǔ)句
If
let adc = 20
if adc <= 32 {
print("adc <= 32")
} else if adc >= 86 {
print("adc >= 86")
} else {
print("32 <adc< 86")
}
Switch
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
不隱式貫穿
Swift中的switch
語(yǔ)句執(zhí)行時(shí)不會(huì)貫穿每個(gè)case澈蟆。而C和Objective-C中的switch
語(yǔ)句需要顯式的break
語(yǔ)句墨辛,若不添加則會(huì)向下貫穿到所匹配項(xiàng)下面的每個(gè)case
,并執(zhí)行趴俘。
MyEnumType type = MyEnumType2;
switch (type) {
case MyEnumType1:
NSLog(@"MyEnumType1");
case MyEnumType2:
NSLog(@"MyEnumType2");
case MyEnumType3:
NSLog(@"MyEnumType3");
}
/*
MyEnumType2
MyEnumType3
*/
Swift中的switch
語(yǔ)句只要第一個(gè)匹配的switch
的case
完成睹簇,整個(gè)switch語(yǔ)句就會(huì)完成執(zhí)行,而不需要顯式的break
語(yǔ)句寥闪。
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
Range匹配
switch
語(yǔ)句可以檢測(cè)其判斷的值带膀,是否包含在某個(gè)case的范圍內(nèi)。
let adc = 20
var apc = 0;
var result = ""
switch adc {
case 0...10:
result = "0...10"
case 11..<20:
result = "11..<20"
case 20...30:
result = "20...30"
default:
result = "beyond of range"
}
print(result)//!< 20...30
元組
switch
語(yǔ)句可以判斷元組的值橙垢,是否符合某個(gè)case垛叨。可以針對(duì)元組不同的值或值的間隔范圍來測(cè)試元組的每個(gè)元素∷栽或者敛纲,使用下劃線字符(通配符)_
來匹配任何可能的值。
let tuples : (Int,String,Int) = (404,"not found",-1)
switch tuples {
case (300,"精準(zhǔn)匹配1",0):
print("精準(zhǔn)匹配1")
case (0...200,"范圍匹配1",-2...10):
print("范圍匹配1")
case (_,_,-2...2):
print("通配符匹配元組前兩個(gè),范圍匹配最后一項(xiàng)")
default:
print("沒有匹配到")
}
值綁定
switch
中case
可以命名它所匹配到的臨時(shí)的常量或變量剂癌, 以便在case
對(duì)應(yīng)的方法中使用淤翔。這種行為稱為值綁定。
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("橫坐標(biāo)\(x)")
case (0, let y):
print("縱坐標(biāo) \(y)")
case let (x, y):
print("任何點(diǎn) (\(x), \(y))")
}
上述switch語(yǔ)句中case(let x佩谷,0)
匹配y
值為0的任何點(diǎn)旁壮,并將點(diǎn)的x
值賦給臨時(shí)常量x
。case(0谐檀,let y)
匹配x
值為0的任何點(diǎn)抡谐,并將點(diǎn)的y
值賦給臨時(shí)常量y
。let(x桐猬,y)
麦撵,聲明了一個(gè)可以匹配任何值的含有兩個(gè)占位符常量的元組。因?yàn)槠ヅ渌锌赡艿氖S嘀道7荆圆恍枰?code>default case來使switch語(yǔ)句窮舉免胃。
Where
switch語(yǔ)句中case可以使用where
子句來檢查其他條件。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x==y:
print("匹配到x與y相同的情況")
case let(x,y) where x == -y:
print("匹配到x與y為相反數(shù)的情況")//!<匹配到x與y為相反數(shù)的情況
default:
print("沒有匹配的結(jié)果")
}
上述示例中switch語(yǔ)句中的case聲明占位符常量x
和y
惫撰,它們暫時(shí)從yetAnotherPoint
獲取元組值羔沙。這些常量用作where
子句的一部分,用于創(chuàng)建動(dòng)態(tài)過濾器厨钻。僅當(dāng)where
子句的條件對(duì)占位符常量x
和y
的計(jì)算結(jié)果為true
時(shí)撬碟,switch語(yǔ)句的case才會(huì)成功匹配當(dāng)前值。
復(fù)合使用
switch
語(yǔ)句中若有多個(gè)case共享的相同方法體莉撇,可以通過在case
之后組合多個(gè)模式呢蛤,每個(gè)模式之間用逗號(hào)隔開來表示。多個(gè)模式中任一個(gè)匹配棍郎,則認(rèn)為這個(gè)case
是匹配的其障。同時(shí)case
之后的多個(gè)模式也可以多行表示。
let someCharater = "e"
switch someCharater {
case "a","o","e","f":
print("事例1涂佃,匹配成功")//!< log
case "b","v","r","h":
print("事例2励翼,匹配成功")
default:
print("未匹配")
}
//復(fù)合事例中的值綁定,綁定的值類型必須匹配辜荠。case (let x, let y), (0, let x)這種是不被允許的 因?yàn)榉椒w中使用x汽抚,y時(shí),若匹配的是 (0, let x)則y 無(wú)效伯病。因?yàn)橄嗤闹到壎☉?yīng)該存在于所有`case`之后的模式中造烁。
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let x, 0), (0, let x)://!< 'x' must be bound in every pattern:'x'必須綁定在每個(gè)模式中
print("匹配成功")
default:
print("未匹配")
}
switch的case
后對(duì)應(yīng)多個(gè)模式的復(fù)合事例中,每個(gè)模式對(duì)應(yīng)的綁定的值類型必須匹配,并且相同的一組值綁定惭蟋,如(let x, let y)
其中x
苗桂,y
稱為一組值綁定,應(yīng)該存在于所有case
之后的模式中告组。
控制轉(zhuǎn)移語(yǔ)句
控制轉(zhuǎn)移語(yǔ)句通過將控制從一段代碼轉(zhuǎn)移到另一段代碼來改變代碼執(zhí)行的順序煤伟。Swift有五個(gè)控制轉(zhuǎn)移語(yǔ)句:
- continue
- break
- fallthrough
- return
- throw
continue
continue
語(yǔ)句:跳出正在執(zhí)行的循環(huán)語(yǔ)句,立即開始執(zhí)行下一次循環(huán)木缝。
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue
}
puzzleOutput.append(character)
}
print(puzzleOutput) //!< break下跳出循環(huán),立即開始下一次迭代便锨,輸出為:grtmndsthnklk
break
break
語(yǔ)句:立即結(jié)束所有控制流語(yǔ)句的執(zhí)行。
循環(huán)語(yǔ)句中的break
:循環(huán)語(yǔ)句中使用break我碟,會(huì)立即結(jié)束循環(huán)的執(zhí)行放案,并在循環(huán)方法體}
之后將控制權(quán)轉(zhuǎn)移給后續(xù)的代碼。
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
break
}
puzzleOutput.append(character)
}
print(puzzleOutput) //!< break下跳出循環(huán)不在開始怎囚,輸出為gr
switch語(yǔ)句中的break
:在switch語(yǔ)句中使用break
卿叽,會(huì)使switch語(yǔ)句立即結(jié)束其執(zhí)行桥胞,并移交控制權(quán)恳守。
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let x, 0), (0, let x):
print("匹配成功")
default:
break
}
貫穿(Fallthrough)
如果需要C或Objective-C的貫穿行為,則可以使用fallthrough
關(guān)鍵字贩虾。
let describe = 5
var description = ""
switch describe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += "I am"
fallthrough
case 18:
description += " bob" //!< case 去掉fallthrough 則輸出I am bob
fallthrough
default:
description += " and you?"
}
print(description)//!< I am bob and you?
標(biāo)簽語(yǔ)句
在Swift中催烘,其他循環(huán)和條件語(yǔ)句中可以嵌套循環(huán)和條件語(yǔ)句,從而創(chuàng)建復(fù)雜的控制流結(jié)構(gòu)缎罢。但是伊群,循環(huán)和條件語(yǔ)句都可以使用break
語(yǔ)句過早地結(jié)束執(zhí)行。因此策精,需要明確break
語(yǔ)句終止的循環(huán)或條件語(yǔ)句舰始,或明確continue
語(yǔ)句應(yīng)該影響哪個(gè)循環(huán)是非常必要的,故會(huì)用到標(biāo)簽語(yǔ)句咽袜。
var result = ""
let adc = 30
forLabel : for item in 9...adc {
switchLabel :switch item {
case 0...10:
result += " 0...10"
break switchLabel //!<swift中break是默認(rèn)的
case 11..<20: //!< 若是11..<20區(qū)間則跳出for循環(huán)丸卷,開始下次迭代
continue forLabel
case 20://!< 若是20 則立即終止for循環(huán)
result += " 終止for循環(huán)"
break forLabel
default:
result += "beyond of range"
}
}
print(result) //!< 0...10 0...10 終止for循環(huán)
提前退出(throw,return)
與if
語(yǔ)句一樣,guard
語(yǔ)句根據(jù)表達(dá)式的布爾值執(zhí)行語(yǔ)句询刹。使用guard語(yǔ)句要求條件必須為true
才能執(zhí)行guard
語(yǔ)句之后的代碼谜嫉。與if
語(yǔ)句不同,guard
語(yǔ)句總是有一個(gè)else
子句 如果條件為false
凹联,則執(zhí)行else
子句中的代碼沐兰。guard
的else
子句不能向下貫穿,必須使用轉(zhuǎn)移控制的語(yǔ)句return
或throw
才能退出作用域蔽挠。
//return
let adc = 30
//! guard方法體不能向下貫穿住闯,需要使用`return`或`throw`退出作用域
guard adc > 30 else {
print("條件不成立")
return //!< 提前結(jié)束了
}
//throw退出作用域
guard adc > 30 else {
print("條件不成立")
throw HttpError.authError
}
檢查API可用性
Swift支持檢查API可用性:編譯器使用SDK中的可用性信息來驗(yàn)證使用的API是否在指定部署目標(biāo)上可用。確保我們不會(huì)在給定部署目標(biāo)上使用不可用的API∧海可以在if
或guard
語(yǔ)句中使用可用性條件進(jìn)行判斷斟叼。
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {
// Fall back to earlier iOS and macOS APIs
}
參考資料:
swift 5.1官方編程指南
推薦文章:
Xcode11 新建工程中的SceneDelegate
iOS App啟動(dòng)優(yōu)化(二)—— 使用“Time Profiler”工具監(jiān)控App的啟動(dòng)耗時(shí)
iOS App啟動(dòng)優(yōu)化(一)—— 了解App的啟動(dòng)流程
iOS WKWebView的基本使用
Swift 5.1 (4) - 集合類型
iOS 解析一個(gè)自定義協(xié)議
iOS13 DarkMode適配(二)
iOS13 DarkMode適配(一)
2019蘋果秋季新品發(fā)布會(huì)速覽