- 固定次数遍历
for index in 1...5 { print("\(index) times 5 is \(index * 5)") }
- 可以使用forin加数组遍历整个数组,也可以在数组后的[]中加入...指定数组内部的遍历范围
- 当循环内不需要循环变量时使用_代替index
- 使用stride设定步长,to不包含右端,through包含
for tickMark in stride(from: 0, to: 60, by: 5) { }
- while适用于循环长度未知的情况
- 一个经典蛇和梯子的游戏逻辑实现
let finalSquare = 25 var board = [Int](repeating: 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 if square < board.count { // if we're still on the board, move up or down for a snake or a ladder square += board[square] } } print("Game over!") //如果使用repeat-while来实现的话,代码如下 repeat { // move up or down for a snake or ladder square += board[square] // roll the dice diceRoll += 1 if diceRoll == 7 { diceRoll = 1 } // move by the rolled amount square += diceRoll } while square < finalSquare print("Game over!")
- switch不同于OC中顺序执行,在执行某一case之后自动退出switch,不需要手动加入break
- case中至少要有一行代码,不能为空
- case中可以包含多条件,中间用逗号分隔
let anotherCharacter: Character = "a" switch anotherCharacter { case "a", "A": print("The letter A") default: print("Not the letter A") } // Prints "The letter A"
- case中可以使用...来做区间判断
- case中可以使用元组,元组中可以使用...做区间判断
let somePoint = (1, 1) switch somePoint { case (0, 0): print("\(somePoint) is at the origin") case (_, 0): print("\(somePoint) is on the x-axis") case (0, _): print("\(somePoint) is on the y-axis") case (-2...2, -2...2): print("\(somePoint) is inside the box") default: print("\(somePoint) is outside of the box") } // Prints "(1, 1) is inside the box"
- case中元组其中某些项可以定义为变量,其余项符合时这些变量项可以用于body中
- case中可以where做条件判断
case let (x, y) where x == y:
- case中可以使用compound
case (let distance, 0), (0, let distance)
- 控制转移语句:continue, break, fallthrough, return, throw
- continue表示立即停止本次循环继续开始下一次循环
- break表示终止该整个control flow,在switch中case的body内部不允许为空,所以使用break
- 使用fallthrough完成C中switch继续执行下一次case的情况
- 使用标签定义循环,使用break或continue加标签表示退出或继续的循环体
gameLoop: while square != finalSquare break gameLoop
- guard类似于if, 不同之处在于guard用于判断某一条件是否为真,如果为真继续,不为真执行else中代码(else多用return直接退出),guard必须带有else。区别if作为专用,用于判断不满足需求条件的情况,利于阅读
//某一元素缺失时 guard let location = person["location"] else { //打印相关缺失内容 print("I hope the weather is nice near you.") //直接退出该方法 return } //当location存在时才会执行,不存在时该代码不会执行 print("I hope the weather is nice in \(location).")
- 使用if #available 判断版本,*表示任意其他平台
if #available(iOS 10, *) { //Use ios 10 APIs on ios } else { //Fall back to earlier ios }
- 代码:
gameLoop: while square != finalSquare { diceRoll += 1 if diceRoll == 7 { diceRoll = 1 } switch square + diceRoll { case finalSquare: // diceRoll will move us to the final square, so the game is over break gameLoop case let newSquare where newSquare > finalSquare: // diceRoll will move us beyond the final square, so roll again continue gameLoop default: // this is a valid move, so find out its effect square += diceRoll square += board[square] } } print("Game over!")
网友评论