// 元组的内容分解 let (statusCode, statusMessage) = http404Error print("The status code is \(statusCode)") // 只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记 let (justTheStatusCode, _) = http404Error print("The status code is \(justTheStatusCode)") // 可以通过下标来访问元组中的单个元素 print("The status code is \(http404Error.0)") // 可以在定义元组的时候给单个元素命名 let http200Status = (statusCode: 200, description: "OK") print("The status code is \(http200Status.statusCode)")
可选类型(optionals)
使用可选类型来处理值可能缺失的情况。
1 2 3 4
let possibleNumber = "123" let convertedNumber = Int(possibleNumber) // convertedNumber 被推测为类型 "Int?", 或者类型 "optional Int" // 因为如果Int()类型转换函数传入的字符串为非法字符串,则会返回nil
nil
1 2 3 4 5 6 7 8 9
// 可以给可选变量赋值为nil来表示它没有值 var serverResponseCode: Int? = 404 // serverResponseCode 包含一个可选的 Int 值 404 serverResponseCode = nil // serverResponseCode 现在不包含值
// 如果声明一个可选常量或者变量但是没有赋值,它们会自动被设置为 nil var surveyAnswer: String? // surveyAnswer 被自动设置为 nil
if convertedNumber != nil { print("convertedNumber has an integer value of \(convertedNumber!).") } // 输出 "convertedNumber has an integer value of 123."
可选绑定(optional binding)
可选绑定可以用在 if 和 while 语句中,这条语句不仅可以用来判断可选类型中是否有值,同时可以将可选类型中的值赋给一个常量或者变量。
1 2 3 4 5 6
iflet actualNumber = Int(possibleNumber) { print("\'\(possibleNumber)\' has an integer value of \(actualNumber)") } else { print("\'\(possibleNumber)\' could not be converted to an integer") } // 输出 "'123' has an integer value of 123"
let somePoint = (1, 1) switch somePoint { case (0, 0): print("(0, 0) is at the origin") case (_, 0): print("(\(somePoint.0), 0) is on the x-axis") case (0, _): print("(0, \(somePoint.1)) is on the y-axis") case (-2...2, -2...2): print("(\(somePoint.0), \(somePoint.1)) is inside the box") default: print("(\(somePoint.0), \(somePoint.1)) is outside of the box") } // 输出 "(1, 1) is inside the box"
值绑定(Value Bindings)
case 分支允许将匹配的值绑定到一个临时的常量或变量,并且在case分支体内使用 —— 这种行为被称为值绑定(value binding),因为匹配的值在case分支体内,与临时的常量或变量绑定。
下面的例子展示了如何在一个(Int, Int)类型的元组中使用值绑定来分类下图中的点(x, y):
1 2 3 4 5 6 7 8 9 10
let anotherPoint = (2, 0) switch anotherPoint { case (let x, 0): print("on the x-axis with an x value of \(x)") case (0, let y): print("on the y-axis with a y value of \(y)") caselet (x, y): print("somewhere else at (\(x), \(y))") } // 输出 "on the x-axis with an x value of 2"
Where
case 分支的模式可以使用where语句来判断额外的条件。
下面的例子把下图中的点(x, y)进行了分类:
1 2 3 4 5 6 7 8 9 10
let yetAnotherPoint = (1, -1) switch yetAnotherPoint { caselet (x, y) where x == y: print("(\(x), \(y)) is on the line x == y") caselet (x, y) where x == -y: print("(\(x), \(y)) is on the line x == -y") caselet (x, y): print("(\(x), \(y)) is just some arbitrary point") } // 输出 "(1, -1) is on the line x == -y"
let someCharacter: Character = "e" switch someCharacter { case"a", "e", "i", "o", "u": print("\(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": print("\(someCharacter) is a consonant") default: print("\(someCharacter) is not a vowel or a consonant") } // 输出 "e is a vowel"