Swift--基础(二)元组 断言 错误处理

元组(tuples

把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型

let http404Error = (404, "Not Found")

let (statusCode, statusMessage) = http404Error

print("The status code is \(statusCode)")

print("The status message is \(statusMessage)")

忽略后面的值

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)")

断言

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)

}

转载于:https://www.cnblogs.com/huoran1120/p/6121911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值