//: Playground - noun: a place where people can play
import UIKit
// # For-in 循環(huán)
// 遍歷一個序列Sequence,比如左閉的Range,Array,Set,String,Dictionary
// 那個用于遍歷的常量,在每次遍歷循環(huán)開始的時候被隱式聲明,每個循環(huán)結(jié)束了,它就不在了
// trick: 用stride(from:to:/through:by:)跳過
for a in stride(from: 0, to: 20, by: 5) {
print(a)
}
for a in stride(from: 0.0, through: 1.0, by: 0.2) {
print(a)
}
// # While 循環(huán)
// 蛇與梯子游戲
// 最大格數(shù)
let finalSquare = 25
// 初始化
var board = [Int](repeatElement(0, count: finalSquare + 1))
// 安置蛇與梯子
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
// 游戲
var square = 0
var diceRoll = 0
while square < finalSquare {
// roll the dice
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
print(square)
if square < board.count {
// if we're still on the board, move up or down for a snake or a ladder
square += board[square]
} // trick: 這是一種比較好的檢查下表越界的方式
}
print("Game over!")
// Repeat-while 語句
// # 條件語句
// switch
// trick: case后用逗號隔開可以符合多個條件
// trick: 區(qū)間匹配
switch 5 {
case ...0:
print("")
default:
print("f")
}
let somePoint = (1, 1) // 感覺元組有點像是廣義上的坐標(biāo)
switch somePoint {
case (0, 0): // 一旦匹配,會忽略后面的case
print("(0, 0) is at the origin")
case (_, 0): // _表明匹配所有可能的值
print("(\(somePoint.0), 0) is on the x-axis")
case (0, let y): // 值綁定
print("(0, \(y)) is on the y-axis")
case (-2...2, -2...2):
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
case let (x, y):
print("(\(x), \(y)) is outside of the box")
}
// trick: where提供額外的判定
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
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): // 已經(jīng)匹配到了所有的情況,所以不需要default
print("(\(x), \(y)) is just some arbitrary point")
}
// 復(fù)合情況同樣可以包含值綁定辛掠。所有復(fù)合情況的模式都必須包含相同的值綁定集合,并且復(fù)合情況中的每一個綁定都得有相同的類型格式。這才能確保無論復(fù)合情況的哪部分匹配了典徘,接下來的函數(shù)體中的代碼都能訪問到綁定的值并且值的類型也都相同落追。
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance): // 相同的綁定名稱
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
// 一般情況值綁定
var b = 2
switch b {
case let a where a > b:
print(a)
case let b: // 相當(dāng)于default
print(b)
print("")
}
// # 控制轉(zhuǎn)移語句***
// continue 語句告訴循環(huán)停止正在做的事情并且再次從頭開始循環(huán)的下一次遍歷吐绵。
// break 語句會立即結(jié)束整個控制流語句.在switch中,可以把break作為整個函數(shù)體來無視該條件(因為Swift的switch必須要窮盡,而有時候某些范圍沒啥用),比如default中
// 使用fallthrough跟著執(zhí)行一下個case或default的代碼塊慧妄,而不執(zhí)行條件檢查
// trick: 在小循環(huán)(or switch)里面中斷大循環(huán),讓結(jié)構(gòu)更加清晰
bigLoop: for i in 1...10 {
switch i {
case 1, 2, 3:
continue bigLoop
case 8:
print("terminate")
break bigLoop
default:
print(i)
}
}
// # 提前退出
// guard 語句來要求一個條件必須是真才能執(zhí)行 guard 之后的語句坎穿。與 if 語句不同展父, guard 語句總是有一個 else 分句, else 分句里的代碼會在條件不為真的時候執(zhí)行。guard語句一般用于提前退出
// guard let 語句只有else 分句,沒有為真的語句
func greet(_ person: [String: String]) {
guard let name = person["name"] else { return } // trick:沒有名字直接函數(shù)結(jié)束
print("Hello \(name)!") // 在外面依然可以使用name
guard let location = person["location"] else {
print("I hope the weather is nice near you.") // 沒有地點就泛泛而談
return
}
print("I hope the weather is nice in \(location)") // 有地點
}
greet(["name": "John"])
greet(["name": "Jane", "location": "Cupertino"])
// # 檢查API的可用性
if #available(iOS 11, *) {
// 只有當(dāng)系統(tǒng)不低于11時玲昧,才執(zhí)行這里的代碼
} else {
// 執(zhí)行舊平臺代碼
}
5.控制流 Control Flow Swift官方文檔——版納的筆記
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門梨睁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鲸睛,“玉大人娜饵,你說我怎么就攤上這事」俦玻” “怎么了箱舞?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長拳亿。 經(jīng)常有香客問我晴股,道長,這世上最難降的妖魔是什么肺魁? 我笑而不...
- 正文 為了忘掉前任电湘,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寂呛。我一直安慰自己怎诫,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布贷痪。 她就那樣靜靜地躺著幻妓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪劫拢。 梳的紋絲不亂的頭發(fā)上肉津,一...
- 文/蒼蘭香墨 我猛地睜開眼分俯,長吁一口氣:“原來是場噩夢啊……” “哼肾筐!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缸剪,我...
- 正文 年R本政府宣布,位于F島的核電站藤树,受9級特大地震影響浴滴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜岁钓,卻給世界環(huán)境...
- 文/蒙蒙 一升略、第九天 我趴在偏房一處隱蔽的房頂上張望微王。 院中可真熱鬧,春花似錦品嚣、人聲如沸骂远。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽激才。三九已至,卻和暖如春额嘿,著一層夾襖步出監(jiān)牢的瞬間瘸恼,已是汗流浹背。 一陣腳步聲響...