Swift语法基础:2 - Swift的控制流简介

前面我们已经大致了解了Swift的一些代码结构, 声明的时候怎么用, 字符串的串联是怎么回事, 现在我们来看看那Swift的控制流, 所谓的控制流就是for-in, for, switch, if, while, do-while, 下面让我们来看例子:


1.for-in的使用

let individualScores = [1, 2, 3, 4, 5, 6]

for i in individualScores {
    println(i)
}
// 打印出来的结果:1, 2, 3, 4, 5, 6

我们也可以在for-in里添加一个判断语句:

var num = 0
for i in individualScores {
    if i < 3{
        num++
        println("num is \(num)")

    } else {
        num--
        println("num is \(num)")
    }
}
// 打印出来的结果: num is 1, num is 2, num is 1, num is 0, num is -1, num is -2

并且我们还可以使用for-in来遍历字典, 这个遍历会非常的爽:

let interestingNumbers = [
        "Prime": [2, 3, 5, 7, 11, 13],
        "Fibonacci": [1, 1, 2, 3, 5, 8],
        "Square": [1, 4, 9, 16, 25],
]
var largest = 0

for (kind, numbers) in interestingNumbers {
    println(kind)
    println(numbers)
}
// 打印出来的结果: Prime
// [2, 3, 5, 7, 11, 13]
// Fibonacci
// [1, 1, 2, 3, 5, 8]
// Square
// [1, 4, 9, 16, 25]

我们也可以只遍历整个字典最大的值:

for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
// 打印出来的结果是:25

2.1if, let还有” ? “嵌套使用

var optionalString: String? = "Hello"
    optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello,\(name)"
}
println("optionalString is \(optionalString),optionalName is \(optionalName),greeting is \(greeting)")

// 打印出来的结果:optionalString is Optional("Hello"),optionalName is Optional("John Appleseed"),greeting is Hello,John Appleseed

当我们改改例子的东西, 出来的结果就不一样了:

var optionalString: String? = "Hello"
    optionalString == nil

var optionalName: String? = "John Appleseed"
    optionalName == nil

var greeting = "Hello!"

if let name = optionalName {
    greeting = "Hello,\(name)"
}
if let name1 = optionalName {
    greeting = "xiaoming"
}
println("optionalString is \(optionalString),optionalName is \(optionalName),greeting is \(greeting)")
// 打印出来的结果:optionalString is Optional("Hello"),optionalName is Optional("John Appleseed"),greeting is xiaoming

3.switch的用法

let vegetable = "red pepper"
switch vegetable {
    case "celery ":
        let vegetableComment = "Add some raisins and make ants on a log."
        println(vegetableComment)
    case "cucumber", "watercress":
        let vegetableComment = "That would make a good tea sandwich."
        println(vegetableComment)
    case let x where x.hasSuffix("pepper"):
        let vegetableComment = "Is it a spicy \(x)?"
        println(vegetableComment)
    default:
        let vegetableComment = "Everything tastes good in soup."
        println(vegetableComment)
}
// 打印出来的结果: Is it a spicy red pepper?

如果我们把default给删除掉就会出现一个错误:

// Switch must be exhaustive, consider adding a default clause
提示说开关一定要有一个default, 否则该开关就不会成立

PS: 在Swift中, Switch执行到匹配的那一行就会自动退出, 并不会继续往下执行, 所以这里就不存在break.


4.while以及do-while使用

var n = 2
while n<100 {
    n=n*2
}

var m = 2
do {
    m=m*2
} while m < 100

println("n is \(n), m is \(m)")
// 打印出来的结果: n is 128, m is 128

5.补充一点知识

var firstForLoop = 0
for i in 0...3{
        firstForLoop += i
    }

var secondForLoop = 0
for var i=0; i<3; ++i {
    secondForLoop += 1
}
println("\(firstForLoop), \(secondForLoop)")
// 打印出来的结果: 6, 3

看到第一个例子的时候, 其实它是和第二个例子是相等的, 这是一个区间, 0 ~ 3这之间, 所以在Swift开发时候, 无论是传统的写法, 还是新的写法都是可以使用的.


好了这次我们就讲到这里, 下次我们继续

posted @ 2015-03-09 21:46  背着吉他去流浪  阅读(225)  评论(0编辑  收藏  举报