SWIFT学习笔记05

1、Swift 无需写break,所以不会发生这种贯穿(fallthrough)的情况。2、//用不到变量名,可用“_”替换
for _ in 1...power
{
    answer *= base
}

3、case 可以匹配更多的类型模式,包括区间匹配(range matching),元组(tuple)和特定类型的描述。
可以这样用case
case 1...3:
naturalCount = "a few"

4、如果存在多个匹配,那么只会执行第一个被匹配到的 case 分支,剩下的能够匹配的 case 分支都会被忽视掉。

5、case值绑定,此例子都没必要用default
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))") } // 输出 "on the x-axis with an x value of 2"

6、//当且仅当where语句的条件为true时,匹配到的 case 分支才会被执行。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint
{
case let (x, y)where x == y:
println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
println("(\(x), \(y)) is just some arbitrary point")

} // 输出 "(1, -1) is on the line x == -y"

2014年07月03日



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值