码迷,mamicode.com
首页 > 编程语言 > 详细

Swift学习笔记- 5.控制流

时间:2015-02-15 20:36:27      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

for 循环

 
Swift 提供两种 for 循环形式:
(1)for-in 用来遍历一个区间(range),序列(sequence),集合(collection),系列(progression)里面所有的元素执行一系列语句。
(2)for 条件递增语句。用来重复一系列语句。
 
for-in
可以使用 for-in 来遍历一个集合里所有的元素。
 
for index in 1...5 {
    println("\(index) times 5 is \(index * 5)")
}
 
上面的例子中,index 是一个每次被自动赋值的常量。在这种情况下,index 在使用前不需要声明,只需要将它包含在循环的声明中,就可以对其进行隐式声明,而无需使用 let 关键字声明。
 
 
如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:
 
let base = 3
let power = 10
var answer = 1

for _ in 1...power {
    answer *= base
}

println("\(base) to the power of \(power) is \(answer)")
 
 
使用 for-in 遍历一个数组:
 
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    println("Hello, \(name)")
}
 
你也可以通过遍历一个字典来访问它的键值对。遍历字典时,字典的每项元素以(key,value)元组的形式返回,你可以在for-in 循环中使用显示的常量名称来解读(key,value)元组。

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    println("\(animalName)s has \(legCount) legs")
}
 
除了数组和字典,你也可以使用 for-in 来遍历字符串中的字符:
 
for character in "Hello" {
    println(character)
}
 
 
for 条件循环
 Swift 提供传统的 C 样式 for 循环:
 
for var index = 0; index < 3; index++ {
    println("index is \(index)")
}
 
格式:
  • for initialization; condition; increment {
    
  •     statements
    
  • }
    
Swift 中的 for 不需要使用圆括号
 
 
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
    
  • }
    
示例:
 
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 consonatnant")
    default:
        println("\(someCharacter) is not a vowel or a consonant")
}
 
Switch 语句必须是完备的。每一个可能的值都必须至少有一个 case 分支与之对应。在 switch 的最后,必须有一个 default 语句。
 
每一个 case 分支都必须包含至少一条语句。下面的代码是无效的:
 
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
    println("The letter A")
default:
    println("Not the letter A")
}
 
一个 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)")
 
 
元组
你可以使用元组在同一个 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 ont 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")
   
}
 
不像 C 语言,Swift 允许多个 case 匹配同一个值。如果存在多个匹配,只会执行第一个被匹配的 case 分支,剩下的 case 分支将会被忽略掉。
 
 
值绑定
 
 case 分支的模式语序将匹配的值绑定到一个临时的厂里那个或变量,这些常量或变量在该 case 分支里就可以被引用了,这称为值绑定。
 
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, let y):
    println("somewhere else at (\(x), \(y))")
}
 
注意:
这个 switch 语句不包含默认分支。这是因为最后一个 case 可以匹配所有值的元组,这使得 switch 语句已经完备了。
 
Where
case 分支的模式可以使用 where 语句来判断额外的条件。
 
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 lone x == -y )")
case let (x, y):
    println("(\(x), \(y)) is just some arbitray")
}
 
控制转移语句
 
switch 语句的 break
当在 switch 中使用 break思,会立即中断该 switch 代码块的执行,并且跳转到该 switch 代码块结束后的第一行代码。
 
这种特性可以被用来匹配或者忽略一个或多个分支。因为 Swift 需要包含所有的分支并且不允许有空的分支,有时为了使意图更明显,需特意匹配或忽略某个分支,当你想忽略某个分支时,可以在该分支内写上 break 语句。
 
let numberSymbol: Character = "三"
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "?", "一", "?":
    possibleIntegerValue = 1
case "2", "?", "二", "?":
    possibleIntegerValue = 2
case "3", "?", "三", "?":
    possibleIntegerValue = 3
case "4", "?", "四", "?":
    possibleIntegerValue = 4
default:
    break
}
if let integerValue = possibleIntegerValue {
    println("The integer value of \(numberSymbol) is \(integerValue)")
} else {
    println("An integer value could not be found for \(numberSymbol)")
}
 
 
贯穿
如果确实想要 C 风格的贯穿的特性,你可以在每个需要该特性的 case 分支中 hi用 fallthrough 关键字。
 
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough
default:
    description += "an integer"
}

println(description)
 
 
 
 
 
 
 
 
 
 

Swift学习笔记- 5.控制流

标签:

原文地址:http://www.cnblogs.com/kangshang/p/4293440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!