For-in Loops in Swift3.0

For 循環(huán)還是比較有意思的钧大。
forLoop.swift

print("for i in 1..<5 runs 4 times")

for i in 1..<5 {
    print(i)
}

print("")

print("for i in 1...5 runs 5 times")

for i in 1...5 {
    print(i)
}

print("")

print("for _ in 1...2 runs 2 times and only care loop count but not the index in loop")

for _ in 1...2 {
    print("printing _ here, it means nothing but loop, you can't use as \\(_)")
}

print("")

print("we can interate an array")

let a = [6, 5, 4, 3, 2, 1]

for i in a {
    print(i)
}

print("")

print("use tuple can get much more info from for in")

let scores = ["Alice": 29, "John": 6, "Yahs": 90]

for (name, score) in scores {
    print("\(name) gets \(score)")
}

print("")

print("only show values that index%2 is zero")

for (index, item) in a.enumerated().filter({
    (index, item) in index % 2 == 0
}) {
    print("[\(index)]\(item) ")
}

print("")

var x = [10, 3, 20, 15, 4]

print("x.sorted() is \(x.sorted())")

print("x.sorted().filter { $0 > 5 } is \(x.sorted().filter { $0 > 5 })")

print("x.sorted().filter { $0 > 5 }.map { $0 * 100 } is \(x.sorted().filter { $0 > 5 }.map { $0 * 100 })")

Terminal運(yùn)行swift forLoop.swift如下:

for i in 1..<5 runs 4 times
1
2
3
4

for i in 1...5 runs 5 times
1
2
3
4
5

for _ in 1...2 runs 2 times and only care loop count but not the index in loop
printing _ here, it means nothing but loop, you can't use as \(_)
printing _ here, it means nothing but loop, you can't use as \(_)

we can interate an array
6
5
4
3
2
1

use tuple can get much more info from for in
Yahs gets 90
Alice gets 29
John gets 6

only show values that index%2 is zero
[0]6 
[2]4 
[4]2

x.sorted() is [3, 4, 10, 15, 20]
x.sorted().filter { $0 > 5 } is [10, 15, 20]
x.sorted().filter { $0 > 5 }.map { $0 * 100 } is [1000, 1500, 2000] 

關(guān)于enumerated()參見官方文檔

“If you need the integer index of each item as well as its value, use the enumerated() method to iterate over the array instead. For each item in the array, the enumerated() method returns a tuple composed of the index and the value for that item. You can decompose the tuple into temporary constants or variables as part of the iteration”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3 beta).” iBooks.

其中filter就是一個(gè)過濾华临,返回一個(gè)bool值幽污。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末忠售,一起剝皮案震驚了整個(gè)濱河市刨沦,隨后出現(xiàn)的幾起案子倍权,更是在濱河造成了極大的恐慌,老刑警劉巖囱修,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赎瑰,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡破镰,警方通過查閱死者的電腦和手機(jī)餐曼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鲜漩,“玉大人源譬,你說我怎么就攤上這事≡兴疲” “怎么了踩娘?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)喉祭。 經(jīng)常有香客問我养渴,道長(zhǎng),這世上最難降的妖魔是什么泛烙? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任理卑,我火速辦了婚禮,結(jié)果婚禮上蔽氨,老公的妹妹穿的比我還像新娘藐唠。我一直安慰自己,他們只是感情好孵滞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布中捆。 她就那樣靜靜地躺著,像睡著了一般坊饶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上殴蓬,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天匿级,我揣著相機(jī)與錄音,去河邊找鬼染厅。 笑死痘绎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肖粮。 我是一名探鬼主播孤页,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼涩馆!你這毒婦竟也來了行施?” 一聲冷哼從身側(cè)響起允坚,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蛾号,沒想到半個(gè)月后稠项,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鲜结,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年展运,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片精刷。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡棠众,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出王带,到底是詐尸還是另有隱情识虚,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布误算,位于F島的核電站仰美,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏儿礼。R本人自食惡果不足惜咖杂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蚊夫。 院中可真熱鬧诉字,春花似錦、人聲如沸知纷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽琅轧。三九已至伍绳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乍桂,已是汗流浹背冲杀。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留睹酌,地道東北人权谁。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像憋沿,于是被迫代替她去往敵國(guó)和親旺芽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 今早 泡菜:我腰好痛(┯_┯) 火鍋:為什么?→_→ 泡菜:睡覺睡得腰痛~ 火鍋:睡覺你都能腰痛→_→ 泡菜:睡覺...
    初九日閱讀 309評(píng)論 0 0
  • 一個(gè)周就這么在不知不覺中度過采章。 今早雖然霧氣籠罩中运嗜,但也注定今天是個(gè)陽光燦爛微風(fēng)輕拂的日子。 為了讓早餐豐富營(yíng)養(yǎng)共缕,...
    明懿媽媽閱讀 268評(píng)論 0 0
  • 中考成績(jī)以中等的成績(jī)考上了縣城里的一所高中洗出。新生報(bào)到的第一天,我的父母也跟其他父母一樣图谷,把我連人帶行李一起運(yùn)到到學(xué)...
    小棉襖呀閱讀 934評(píng)論 0 2
  • 你現(xiàn)在的氣質(zhì)里便贵,藏著你走過的路菠镇,讀過的書,寫過的字承璃。 哪怕你沒有富庶的日子利耍,也會(huì)有富庶的生命,強(qiáng)大的心理盔粹。 毛姆說...
    吳蕙蘭閱讀 2,480評(píng)論 0 0