For 循环

for 循环用来按照指定的次数多次执行一系列语句。Swift 提供两种 for 循环形式:
 
for-in 用来遍历一个范围(range),队列(sequence),集合(collection),系列(progression)里面所有的元素执行一系列语句。
 
for 条件递增语句(for-condition-increment),用来重复执行一系列语句直到特定条件达成,一般通过在每次循环完成后增加计数器的值来实现。
 

For-In

下面的例子用来输出乘5乘法表前面一部分内容:
for index in 1...5 {
  println("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
 
如果你不需要知道范围内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
  answer *= base
}
println("\(base) to the power of \(power) is \(answer)")
// prints "3 to the power of 10 is 59049
 
使用 for-in 遍历一个数组所有元素:
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
  println("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!

 

你也可以通过遍历一个字典来访问它的键值对(key-value pairs)。遍历字典时,字典的每项元素会以 (key, value)元组的形式返回,你可以在 for-in 循环中使用显式的常量名称来解读 (key, value)元组。下面的例子中,字典的键(key)解读为常量 animalName ,字典的值会被解读为常量 legCount :
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
  println("\(animalName)s have \(legCount) legs")
}
// spiders have 8 legs
// ants have 6 legs
// cats have 4 legs

 

字典元素的遍历顺序和插入顺序可能不同,字典的内容在内部是无序的,所以遍历元素时不能保证顺序。

 

除了数组和字典,你也可以使用 for-in 循环来遍历字符串中的字符:

for character in "Hello" {
  println(character)
}
// H
// e
// l
// l
// o

 

For条件递增(for-condition-increment)

除了 for-in 循环,Swift 提供使用条件判断和递增方法的标准C样式 for 循环:
for var index = 0; index < 3; ++index {
println("index is \(index)")
}
// index is 0
// index is 1
// index is 2

 

下面是一般情况下这种循环方式的格式:

for initialization; condition; increment {
    statements
} 

 

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

 

var index: Int
for index = 0; index < 3; ++index {
    println("index is \(index)")
}
// index is 0
// index is 1
// index is 2
println("The loop statements were executed \(index) times")
// prints "The loop statements were executed 3 times

 

While 循环

While 循环运行一系列语句直到条件变成 false。这类循环适合使用在第一次迭代前迭代次数未知的情况下。Swift 提供两种 while 循环形式:
  - while 循环,每次在循环开始时计算条件是否符合;
  - do-while 循环,每次在循环结束时计算条件是否符合。

While

While 循环从计算单一条件开始。如果条件为 true,会重复运行一系列语句,直到条件变为false。
下面是一般情况下 while 循环格式:
while condition {
  statements
}

 

Do-While

while 循环的另外一种形式是 do-while,它和 while 的区别是在判断循环条件之前,先执行一次循环的代码块,然后重复循环直到条件为 false。
 
下面是一般情况下 do-while 循环的格式:
do {
   statements
} while condition 

条件语句

Swift 提供两种类型的条件语句:if语句和switch语句。

If

if语句最简单的形式就是只包含一个条件,当且仅当该条件为真时,才执行相关代码:
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
  println("It's very cold. Consider wearing a scarf.")
}
// prints "It's very cold. Consider wearing a scarf."

上面的例子会判断温度是否小于等于32华氏度(水的冰点)。如果是,则打印一条消息;否则,不打印任何消息,继续执行if块后面的代码。

 

当然,if语句允许二选一,也就是当条件为假时,执行else语句:
temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
    println("It's very cold. Consider wearing a scarf.")
} else {
    println("It's not that cold. Wear a t-shirt.")
}
// prints "It's not that cold. Wear a t-shirt."

 

显然,这两条分支中总有一条会被执行。由于温度已升至40华氏度,不算太冷,没必要再围围巾——因此,else分支就被触发了。

 

你可以把多个if语句链接在一起,像下面这样:

temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
    println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
    println("It's really warm. Don't forget to wear sunscreen.")
} else {
    println("It's not that cold. Wear a t-shirt.")
}
// prints "It's really warm. Don't forget to wear sunscreen."

在上面的例子中,额外的if语句用于判断是不是特别热。而最后的else语句被保留了下来,用于打印既不冷也不热时的消息。

 

实际上,最后的else语句是可选的:
temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
  println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
  println("It's really warm. Don't forget to wear sunscreen.")
}
在这个例子中,由于既不冷也不热,所以不会触发if或else if分支,也就不会打印任何消息。
 

Switch

switch语句会尝试把某个值与若干个模式(pattern)进行匹配。根据第一个匹配成功的模式,switch语句会执行对应的代码。当有可能的情况较多时,通常用switch语句替换if语句。
 
switch语句最简单的形式就是把某个值与一个或若干个相同类型的值作比较:
switch `some value to consider` {
case `value 1`:
    `respond to value 1`
case `value 2`,`value 3`:
    `respond to value 2 or 3`
default:
`otherwise, do something else`
}

 

每一个case都是代码执行的一条分支,这与if语句类似。与之不同的是,switch语句会决定哪一条分支应该被执行。
 
switch语句必须是完备的。这就是说,每一个可能的值都必须至少有一个case块与之对应。在某些不可能涵盖所有值的情况下,你可以使用默认(default)块满足该要求,这个默认块必须在switch语句的最后面。

 

下面的例子使用switch语句来匹配一个名为someCharacter的小写字符:

let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
  println("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m","n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
  println("\(someCharacter) is a consonant")
default:
  println("\(someCharacter) is not a vowel or a consonant")
}
// prints "e is a vowel"
在这个例子中,第一个case块用于匹配五个元音,第二个case块用于匹配所有的辅音。
由于为其它可能的字符写case快没有实际的意义,因此在这个例子中使用了默认块来处理剩下的既不是元音也不是辅音的字符——这就保证了switch语句的完备性。

不存在隐式的贯穿(Fallthrough)

与C语言和Objective-C中的switch语句不同,在 Swift 中,当匹配的case块中的代码执行完毕后,程序会终止switch语句,而不会继续执行下一个case块。这也就是说,不需要在case块中显式地使用break语句。这使得switch语句更安全、更易用,也避免了因忘记写break语句而产生的错误。
 
 
每一个case块都必须包含至少一条语句。像下面这样书写代码是无效的,因为第一个case块是空的:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
    println("The letter A")
default:
    println("Not the letter A")
}
// this will report a compile-time error

 

不像C语言里的switch语句,在 Swift 中,switch语句不会同时匹配"a"和"A"。相反的,上面的代码会引起编译期错误:case "a": does not contain any executable statements——这就避免了意外地从一个case块贯穿到另外一个,使得代码更安全、也更直观。
 
一个case也可以包含多个模式,用逗号把它们分开(如果太长了也可以分行写):
switch `some value to consider` {
case `value 1`,`value 2`:
  `statements`
}
注意:如果想要贯穿特定的case块中,请使用fallthrough语句

 

范围匹配

case块的模式也可以是一个值的范围。下面的例子展示了如何使用范围匹配来输出任意数字对应的自然语言格式:
let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
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"
}
println("There are \(naturalCount) \(countedThings).")
// prints "There are millions and millions of stars in the Milky Way."

 

元组 (Tuple)

你可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是范围。另外,使用下划线(_)来匹配所有可能的值。
 
下面的例子展示了如何使用一个(Int, Int)类型的元组来分类下图中的点(x, y):
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")
}
// prints "(1, 1) is inside the box"

不像C语言,Swift 允许多个case匹配同一个值。实际上,在这个例子中,点(0, 0)可以匹配所有四个case。但是,如果存在多个匹配,那么只会执行第一个被匹配到的case块。考虑点(0, 0)会首先匹配case (0, 0),因此剩下的能够匹配(0, 0)的case块都会被忽视掉。

 

值绑定 (Value Bindings)

case块的模式允许将匹配的值绑定到一个临时的常量或变量,这些常量或变量在该case块里就可以被引用了——这种行为被称为值绑定。
 
下面的例子展示了如何在一个(Int, Int)类型的元组中使用值绑定来分类下图中的点(x, y):
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))")
}
// prints "on the x-axis with an x value of 2"

在上面的例子中,switch语句会判断某个点是否在红色的x轴上,是否在黄色y轴上,或者不在坐标轴上。
 
这三个case都声明了常量x和y的占位符,用于临时获取元组anotherPoint的一个或两个值。第一个case——case (let x, 0)将匹配一个纵坐标为0的点,并把这个点的横坐标赋给临时的常量x。类似的,第二个case——case (0, let y)将匹配一个横坐标为0的点,并把这个点的纵坐标赋给临时的常量y。
 
一旦声明了这些临时的常量,它们就可以在其对应的case块里引用。在这个例子中,它们用于简化println的书写。
 
请注意,这个switch语句不包含默认块。这是因为最后一个case——case let(x, y)声明了一个可以匹配余下所有值的元组。这使得switch语句已经完备了,因此不需要再书写默认块。
 
在上面的例子中,x和y是常量,这是因为没有必要在其对应的case块中修改它们的值。然而,它们也可以是变量——程序将会创建临时变量,并用相应的值初始化它。修改这些变量只会影响其对应的case块。

Where

case块的模式可以使用where语句来判断额外的条件。
 
下面的例子把下图中的点(x, y)进行了分类:
let 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")
}
// prints "(1, -1) is on the line x == -y"

在上面的例子中,switch语句会判断某个点是否在绿色的对角线x == y上,是否在紫色的对角线x == -y上,或者不在对角线上。
 
这三个case都声明了常量x和y的占位符,用于临时获取元组yetAnotherPoint的两个值。这些常量被用作where语句的一部分,从而创建一个动态的过滤器(filter)。当且仅当where语句的条件为真时,匹配到的case块才会被执行。
 
就像是值绑定中的例子,由于最后一个case块匹配了余下所有可能的值,switch语句就已经完备了,因此不需要再书写默认块。
 

来源:

 

2015-03-19

21:37:22

posted on 2015-03-19 21:42  道无涯  阅读(140)  评论(0编辑  收藏  举报