Swift learn 3

来源:互联网 发布:用友软件使用说明 编辑:程序博客网 时间:2024/05/21 17:59

Swift基础学习

// Playground - noun: a place where people can playimport UIKit//如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问for _ in 1...5{    println("this is _");}for var i=0; i<5; ++i{    println("value:\(i)");}var num = 10;while num-- > 0 {    println(num);}//switch语句必须是完备的。这就是说,每一个可能的值都必须至少有一个 case 分支与之对应。在某些不可能涵盖所有值的情况下var name:String = "Michael";switch name{case "MZ":    println("This is MZ");    case "Michael":    println(name);    default:    println("default");}//case 分支的模式也可以是一个值的区间var testNum = 10000;switch testNum{case 0...9:    println("0...9");    case 10...9998:    println("10...9998");    case 9999...100000:    println("9999...100000");    default:    println("default");}//你可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。//这个判断坐标很有效let points = (1, 1)switch points {case (0, 0):    println("(0, 0) is at the origin")case (_, 0):    println("(\(points.0), 0) is on the x-axis")case (0, _):    println("(0, \(points.1)) is on the y-axis")case (-2...2, -2...2):    println("(\(points.0), \(points.1)) is inside the box")default:    println("(\(points.0), \(points.1)) is outside of the box")}//case 分支的模式可以使用where语句来判断额外的条件。let point0 = (1, -1)switch point0 {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) where x*x == y:    println("(\(x), \(y)) is on the line x*x == y");    case let (x, y):    println("(\(x), \(y)) is just some arbitrary point");default:    println("default value");}//控制转移语句改变你代码的执行顺序,通过它你可以实现代码的跳转。Swift有四种控制转移语句。//continue//break//fallthrough Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。//return


0 0
原创粉丝点击