Swift学习这十二:(续)控制流

  1. // 值绑定(Value Binding)  
  2. let anotherPoint = (20)  
  3. // 这就是所谓的值绑定,通过值赋给临时常量或者变量  
  4. switch anotherPoint {  
  5.   case (let x, 0): // 这里不需要修改x的值,所以声明为let,即常量  
  6.     println("on the x-axis with an x value of \(x)")  
  7.   case (0, let y):  
  8.     println("on the y-axis with a y value of \(y)")  
  9.   case let (x, y): // 对于这里,没有使用Default,其实这里这么写法就相当于default:  
  10.     println("somewhere else at (\(x), \(y))")    
  11. }  
  12.   
  13. // 使用where语句来检测额外的条件  
  14. let yetAnotherPoint = (1, -1)  
  15. switch yetAnotherPoint {  
  16.   case let (x, y) where x == y// 使用值绑定,要求x与y相等  
  17.     println("(\(x), \(y)) is on the line x == y")  
  18.   case let (x, y) where x == -y:// 使用值绑定,要求x与-y相等  
  19.     println("(\(x), \(y)) is on the line x == -y")   
  20.   case let (x, y):// 使用值绑定,相当于default  
  21.     println("(\(x), \(y)) is just some arbitrary point")   
  22. }  
  23.   
  24. /* 
  25.   continue 
  26.   break 
  27.   falthrough 
  28.   return 
  29. */  
  30. // continue、break、return跟C、OC中的continue、break、return是一样的  
  31. let puzzleInput = "great minds think alike"  
  32. var puzzleOutput = ""  
  33. for c in puzzleInput {  
  34.   switch c {  
  35.     case "a""e""i""o""u""":// 相当于遇到这几种字符就会就会继续循环而不往下执行  
  36.        continue  
  37.     default:  
  38.        puzzleOutput += c      
  39.   }  
  40. }  
  41.   
  42. let numberSymbol: Character = "三"  
  43. var possibleIntegerValue: Int?  
  44. switch numberSymbol {  
  45.   case "1""一":  
  46.     possibleIntegerValue = 1  
  47.   case "2""二"   
  48.   default:  
  49.     break  
  50. }  
  51.   
  52. let integerToDescribe = 5  
  53. var descripton = "The number \(integerToDescribe) is"  
  54. switch integerToDescribe {  
  55.   case 23579111317, 19:  
  56.     descripton += "a prime number, and also"  
  57.     falthrough // 加上falthrough,就会继续往下执行,执行default这里的语句  
  58.   default:  
  59.     descripton += " an integer"  
  60. }  
  61. // print: The number 5 is a prime number, and also an integer   
  62.   
  63. // 可以给循环添加标签  
  64. var integerValue = 0  
  65. let count = 10  
  66. GameLoopLabel: while integerValue < count {  
  67.   switch integerValue {  
  68.     case integerValue % 2 == 0:   
  69.       break GameLoopLabel // 调用此语句后,就退出了while循环  
  70.     case let inValue where (inValue > 5 && inValue % 2 != 0):  
  71.       continue GameLoopLabel  
  72.     default:  
  73.       println("run default")  
  74.       break GameLoopLabel  
  75.   }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值