标签:
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
let names = ["Anna", "Alex", "Brian", "Jack"] for name in names { println("Hello, \(name)!") } // Hello, Anna! // Hello, Alex! // Hello, Brian! // Hello, Jack!
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 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 condition { statements }
do { statements } while condition
Swift 提供两种类型的条件语句:if语句和switch语句。
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块后面的代码。
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."
你可以把多个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语句被保留了下来,用于打印既不冷也不热时的消息。
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.") }
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` }
下面的例子使用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"
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
switch `some value to consider` { case `value 1`,`value 2`: `statements` }
范围匹配
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."
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块都会被忽视掉。
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"
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"
来源:
2015-03-19
21:37:22
标签:
原文地址:http://www.cnblogs.com/huangzx/p/4351862.html