首页 > 编程语言 > 详细

SWIFT学习笔记05

时间:2017-08-15 13:05:48      阅读:218      评论:0      收藏:0      [点我收藏+]
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日



SWIFT学习笔记05

原文:http://www.cnblogs.com/clnchanpin/p/7364482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!