首页 > 代码库 > swift 运算符和控制流程

swift 运算符和控制流程

闭区间运算符

闭区间运算符(a...b)定义一个包含从a到b(包括a和b)的所有值的区间,只能是数字

for index in 1...5 {    println("\(index) * 5 = \(index * 5)")}var names = ["Anna", "Alex", "Brian", "Jack"]names[2...3] = ["a","b"] //不能添加

半闭区间

半闭区间(a..b)定义一个从a到b但不包括b的区间。方便取数组下标

let names = ["Anna", "Alex", "Brian", "Jack"]let count = names.countfor i in 0..count {    println("第 \(i + 1) 个人叫 \(names[i])")}for i in 0...count-1{    println("第 \(i + 1) 个人叫 \(names[i])")}

闭区间和半闭区间代替了传统的.for循环.使用i++的形式,还可以用于再数组上

fon-in

for-in用来遍历一个区间(range),序列(sequence),集合(collection),系列(progression)里面所有的元素执行一系列语句。

//区间for index in 1...5 {    println("\(index) times 5 is \(index * 5)")}//数组let names = ["Anna", "Alex", "Brian", "Jack"]for name in names {    println("Hello, \(name)!")}//字典let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs {    println("\(animalName)s have \(legCount) legs")}//字符for character in "Hello" {    println(character)}

for-in中index是一个每次循环遍历开始时被自动赋值的常量。这种情况下,index在使用前不需要声明,只需要将它包含在循环的声明中 ,就可以对其进行隐式声明,而无需使用let关键字声明。
index常量只存在于循环的生命周期里。如果你想在循环完成后访问index的值,又或者想让index成为一个变量而不是常量,你必须在循环之前自己进行声明。

for循环

在初始化表达式中声明的常量和变量(比如var index = 0)只在for循环的生命周期里有效。如果想在循环结束后访问index的值,你必须要在循环生命周期开始前声明index。

for initialization; condition; increment {statements}

等同于

  initializationwhile condition {statementsincrement}

switch

switch当匹配后,不会继续执行下一个case,会终止switch语句,所以不需要break语句.如果想要贯穿至特定的 case 分支中,请使用fallthrough贯穿语句
每一个 case 分支都必须包含至少一条语句。代替可以使用","匹配多个case,为同一个值

let someCharacter: Character = "e"switch someCharacter {case "a", "e", "i", "o", "u":    println("\(someCharacter) is a vowel")case "b", "c", "d",:    println("\(someCharacter) is a consonant")default:    println("\(someCharacter) is not a vowel or a consonant")}//case 条件可以是区间,查找一个数字是否在一个区间switch count {case 0:    naturalCount = "no"case 1...3:    naturalCount = "a few"case 4...9:    naturalCount = "several"case 10...99:    naturalCount = "tens of"case 100...999:    naturalCount = "hundreds of"case 1000...999_999:    naturalCount = "thousands of"default:    naturalCount = "millions and millions of"}使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。let somePoint = (1, 1)switch somePoint {case (0, 0):    println("(0, 0) is at the origin")case (_, 0):    println("(\(somePoint.0), 0) is on the x-axis")case (0, _):    println("(0, \(somePoint.1)) is on the y-axis")case (-2...2, -2...2):    println("(\(somePoint.0), \(somePoint.1)) is inside the box")default:    println("(\(somePoint.0), \(somePoint.1)) is outside of the box")}//值绑定let anotherPoint = (2, 0)switch anotherPoint {case (let x, 0):    println("on the x-axis with an x value of \(x)")case (0, let y):    println("on the y-axis with a y value of \(y)")case let (x, y):    println("somewhere else at (\(x), \(y))")}// 输出 "on the x-axis with an x value of 2"//使用wherelet yetAnotherPoint = (1, -1)switch yetAnotherPoint {case let (x, y) where x == y:    println("(\(x), \(y)) is on the line x == y")case let (x, y) where x == -y:    println("(\(x), \(y)) is on the line x == -y")case let (x, y):    println("(\(x), \(y)) is just some arbitrary point")}// 输出 "(1, -1) is on the line x == -y"

Label

使用break和continue在多重循环或者switch嵌套中跳转

  gameLoop: while square != finalSquare {    if ++diceRoll == 7 { diceRoll = 1 }    switch square + diceRoll {    case finalSquare:        // 到达最后一个方块,游戏结束        break gameLoop    case let newSquare where newSquare > finalSquare:        // 超出最后一个方块,再掷一次骰子        continue gameLoop    default:        // 本次移动有效        square += diceRoll        square += board[square]    }}println("Game over!")