前言
在swift中溶诞,我們經(jīng)常會(huì)用到的兩種快速遍歷的方法灵疮,一種是最常見也是最常用的for ... in ...
, 另外一種也是不同于Objective-C的forEach
贪磺。那么逢防,兩者的使用有什么區(qū)別呢雕擂?今天,讓我們來聊一聊swift中遍歷方法的那些事产徊。
for in 與 forEach
同類型的泛型集合
- for in
let array = ["1", "2", "3", "4", "5"]
for element in array {
print(element)
}
打印結(jié)果: 1, 2, 3, 4, 5
- forEach
let array = ["1", "2", "3", "4", "5"]
array.forEach { (element) in
print(element)
}
打印結(jié)果: 1, 2, 3, 4, 5
在集合的元素類型相同(比如上面的數(shù)組是String類型)的情況下,兩者遍歷效果相同蜀细,方便、敏捷,我們可以隨意選用坚冀。
不同類型元素的集合
- for in
let array = [1, 2, 3, "cat", "rabbit"] as [Any] // as [Any]是swift 3的語法要求戚绕,因?yàn)閿?shù)組中有兩種不同類型的元素,分別是:Int 归斤、String痊夭, 所以需要轉(zhuǎn)化成 [Any]類型
for element in array {
print(element)
}
打印結(jié)果:1, 2脏里, 3她我, cat, rabbit
- forEach
let array = [1, 2, 3, "cat", "rabbit"] as [Any]
array.forEach { (element) in
print(element)
}
打印結(jié)果:1, 2番舆, 3酝碳, cat, rabbit
在集合的元素類型不相同(比如上面的數(shù)組是Int和String類型)的情況下恨狈,兩者遍歷效果相同疏哗,方便、敏捷禾怠,我們可以也隨意選用返奉。
return關(guān)鍵字
- for in
let array = ["1", "2", "3", "4", "5"]
for element in array {
if element == "3" {
return
}
print(element)
}
print("Hello World")
打印結(jié)果:1, 2
- forEach
let array = ["1", "2", "3", "4", "5"]
array.forEach { (element) in
if element == "3" {
return
}
print(element)
}
print("Hello World")
打印結(jié)果:1吗氏, 2芽偏, 4, 5牲证, Hello World
在使用return關(guān)鍵字的時(shí)候哮针,很明顯,
for in
中是當(dāng)符合當(dāng)前執(zhí)行語句時(shí)坦袍,程序直接終止到此并返回, 比如上面的元素 "4"十厢、"5"、"Hello World" 沒有被執(zhí)行捂齐;而forEach
中是當(dāng)符合當(dāng)前執(zhí)行語句時(shí)蛮放,程序跳過本次判斷繼續(xù)執(zhí)行, 比如上面的元素"4"奠宜、"5"包颁、"Hello World"被執(zhí)行。
continue關(guān)鍵字
- for in
let array = ["1", "2", "3", "4", "5"]
for element in array {
if element == "3" {
continue
}
print("element is \(element)")
}
print("Test \"continue\"")
打印結(jié)果:
element is 1
element is 2
element is 4
element is 5
Test "continue"
- forEach
let array = ["1", "2", "3", "4", "5"]
array.forEach { (element) in
if element == "3" {
continue
}
print(element)
}
print("Test \"continue\"")
錯(cuò)誤: 程序根本不能執(zhí)行
error:continue is only allowed inside a loop
在使用continue關(guān)鍵字的時(shí)候压真,
for in
可以正常遍歷并且執(zhí)行娩嚼,而且 continue的作用是跳出本次循環(huán),不影響后面的執(zhí)行滴肿; 而在forEach
中岳悟,swift是不允許這樣執(zhí)行的,報(bào)錯(cuò)的原因是說 continue只允許出現(xiàn)在循環(huán)語句中泼差,也就是說不能使用在forEach
的closure中贵少。
break關(guān)鍵字
- for in
let array = ["1", "2", "3", "4", "5"]
for element in array {
if element == "3" {
break
}
print("element is \(element)")
}
print("Test \"continue\"")
打印結(jié)果:
element is 1
element is 2
Test "continue"
- forEach
let array = ["1", "2", "3", "4", "5"]
array.forEach { (element) in
if element == "3" {
break
}
print(element)
}
print("Test \"continue\"")
錯(cuò)誤:程序根本不能執(zhí)行
error:Unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do
在break關(guān)鍵字中,對(duì)于
for in
來說是可以的堆缘,跳出本層循環(huán)滔灶,也就是for循環(huán),然后繼續(xù)執(zhí)行后面的程序吼肥; 對(duì)于forEach
來說录平,同continue關(guān)鍵字的效果一樣麻车,swift不允許這樣使用,原因說的是break只能用于循環(huán)語句或switch語句萄涯,break會(huì)退出本層循環(huán)語句绪氛。
Apple官方對(duì) forEach 的說明
下面是Apple的官方文檔解釋,對(duì)forEach遍歷方法做了個(gè)大致的介紹涝影,有興趣可以看一下
/// Calls the given closure on each element in the sequence in the same order
/// as a `for`-`in` loop.
///
/// The two loops in the following example produce the same output:
///
/// let numberWords = ["one", "two", "three"]
/// for word in numberWords {
/// print(word)
/// }
/// // Prints "one"
/// // Prints "two"
/// // Prints "three"
///
/// numberWords.forEach { word in
/// print(word)
/// }
/// // Same as above
///
/// Using the `forEach` method is distinct from a `for`-`in` loop in two
/// important ways:
///
/// 1. You cannot use a `break` or `continue` statement to exit the current
/// call of the `body` closure or skip subsequent calls.
/// 2. Using the `return` statement in the `body` closure will exit only from
/// the current call to `body`, not from any outer scope, and won't skip
/// subsequent calls.
///
/// - Parameter body: A closure that takes an element of the sequence as a
/// parameter.
</br>
小結(jié):
- for in 能使用 return枣察、break、continue關(guān)鍵字燃逻,forEach不能使用 break序目、continue關(guān)鍵字
- for in 和 forEach 在 return關(guān)鍵字 的使用上有著本質(zhì)的區(qū)別
- 一般情況下,兩者都可通用伯襟,都方便猿涨、敏捷
- for in 使用范圍比 forEach更廣
</br>
歡迎加入 iOS(swift)開發(fā)互助群:QQ群號(hào):558179558, 相互討論和學(xué)習(xí)姆怪!你想要的答案這里都有...