Swift(十一)控制流程

2016-10-30  本文已影响29人  冰三尺
aa5804740e5e3d7319d44c5f8eb23b7322295bc424938-8GPdjE_fw658.jpeg

使用for in 循环字典

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}
//for in
for index in 0 ..< 3 {
    print("index is \(index)")
}
swift2.0
for  character in "dog" {
    print(character)
}
swift3.0会报错 type 'String' does not conform to protocol 'Sequence'
作为的 Swift 3, String 不符合 SequenceType 。但是,您可以使用 characters 属性 String.characters 返回 String.CharacterView ,顺应了 SequenceType ,所以可以用遍历 for 循环:
//正确做法
1. 转换
for  character in "dog".characters {
    print(character)
}
2.您可以添加到扩展 String ,使其符合 SequenceType :
extension String: SequenceType {}  
for i in "Zebra" {  
    print(i)  
}  

while循环

var  j = 0
while j < 3 {
    j += 1
    print(j)
}

do-while 的格式:在swift3里面修改成repeat-while

var  j = 0
repeat {
    j += 1
    print(j)
} while j < 3

switch-case(非隐式贯穿)
Swift 的 switch 与 C 语言和 Objective-C 是不同的。当匹配的 case 分支中的代码执行完毕后,switch就终止了,不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用 break 语句。这使得 switch 语句更安全、更易用,也避免了因忘记写 break 语句而产生的错误。

let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
    print("the letter a")
case "A":
    print("The letter A")
default:
    print("Not the letter A")
}

如果在switch-case中, 某一个条件符合,但是没有对应的执行语句则会报错, 此时需要使用break提前结束执行

let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a": break
case "A":
    print("The letter A")
default:
    print("Not the letter A")
}

switch-case(范围匹配)
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."

元组
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")
}
// prints "(1, 1) is inside the box"

Swift 允许多个 case 匹配同一个值。但是只会获取到最先匹配的case, 其他的case都会被忽略掉


值绑定
值绑定(value binding):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, y):
    println("somewhere else at (\(x), \(y))")
}
// prints "on the x-axis with an x value of 2"

这三个 case 都声明了常量 x 和 y 的占位符,用于临时获取元组 anotherPoint 的一个或两个值, x和y可以代表任意值.


where语句
case 分支模式可以使用 where 语句判断额外的条件。

let yetAnotherPoint = (1, -1)
        switch yetAnotherPoint {
        case let (x, y) where x == y:
            print("(\(x), \(y)) is on the line x == y")
        case let (x, y) where x == -y:
            print("(\(x), \(y)) is on the line x == -y")
        case let (x, y):
            print("(\(x), \(y)) is just some arbitrary point")
        }
        // prints "(1, -1) is on the line x == -y"

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

上一篇 下一篇

猜你喜欢

热点阅读