元組(tuples)less
把多個值組合成一個複合值。元組內的值能夠是任意類型,並不要求是相同類型spa
let http404Error = (404, "Not Found")code
let (statusCode, statusMessage) = http404Errorip
print("The status code is \(statusCode)")io
print("The status message is \(statusMessage)")class
忽略後面的值程序
let (justTheStatusCode, _) = http404Error 命名
print("The status code is \(justTheStatusCode)")di
經過下標來訪問元組中的單個元素make
print("The status code is \(http404Error.0)")
能夠在定義元組的時候給單個元素命名
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")
斷言
let age = -3
assert(age >= 0, "A person's age cannot be less than zero")
會致使程序的終止
錯誤處理
func makeASandwich() throws {
// ...
}
do {
try makeASandwich()
eatASandwich()
} catch SandwichError.outOfCleanDishes {
washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
buyGroceries(ingredients)
}