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

简介:
// 值绑定(Value Binding)
let anotherPoint = (2, 0)
// 这就是所谓的值绑定,通过值赋给临时常量或者变量
switch anotherPoint {
  case (let x, 0): // 这里不需要修改x的值,所以声明为let,即常量
    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): // 对于这里,没有使用Default,其实这里这么写法就相当于default:
    println("somewhere else at (\(x), \(y))")  
}

// 使用where语句来检测额外的条件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
  case let (x, y) where x == y: // 使用值绑定,要求x与y相等
    println("(\(x), \(y)) is on the line x == y")
  case let (x, y) where x == -y:// 使用值绑定,要求x与-y相等
    println("(\(x), \(y)) is on the line x == -y") 
  case let (x, y):// 使用值绑定,相当于default
    println("(\(x), \(y)) is just some arbitrary point") 
}

/*
  continue
  break
  falthrough
  return
*/
// continue、break、return跟C、OC中的continue、break、return是一样的
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for c in puzzleInput {
  switch c {
    case "a", "e", "i", "o", "u", "":// 相当于遇到这几种字符就会就会继续循环而不往下执行
	   continue
	default:
       puzzleOutput += c	
  }
}

let numberSymbol: Character = "三"
var possibleIntegerValue: Int?
switch numberSymbol {
  case "1", "一":
    possibleIntegerValue = 1
  case "2", "二"	
  default:
    break
}

let integerToDescribe = 5
var descripton = "The number \(integerToDescribe) is"
switch integerToDescribe {
  case 2, 3, 5, 7, 9, 11, 13, 17, 19:
    descripton += "a prime number, and also"
	falthrough // 加上falthrough,就会继续往下执行,执行default这里的语句
  default:
	descripton += " an integer"
}
// print: The number 5 is a prime number, and also an integer 

// 可以给循环添加标签
var integerValue = 0
let count = 10
GameLoopLabel: while integerValue < count {
  switch integerValue {
    case integerValue % 2 == 0: 
	  break GameLoopLabel // 调用此语句后,就退出了while循环
	case let inValue where (inValue > 5 && inValue % 2 != 0):
	  continue GameLoopLabel
	default:
	  println("run default")
	  break GameLoopLabel
  }
}

目录
打赏
0
0
0
0
41
分享
相关文章
Swift的控制流语句
Swift的控制流语句
85 3
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
Swift 的控制流结构
Swift 的控制流结构
77 1
深入浅出Swift(2)—— 控制流
深入浅出Swift(2)—— 控制流
106 0
(一)swift的基本学习
(一)swift的基本学习
226 0
Swift学习第二练——Swift项目时光电影
Swift学习第二练——Swift项目时光电影
198 0
Swift学习第二练——Swift项目时光电影
Swift学习第一练——用Swift实现的FlappyBird小游戏
Swift学习第一练——用Swift实现的FlappyBird小游戏
235 0
Swift学习第一练——用Swift实现的FlappyBird小游戏
Swift学习第十三枪-使用Swift开发IOS中蓝牙4.0的开发流程
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
1138 0
Swift学习第十二枪----移动数据库王者Realm入门教程
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51636392 今天给大家带来的是堪称是一个可以替代SQLite,Core Data 的以及ORMlibraries的轻量级数据库—Realm移动端数据库。
1014 0
Swift学习第十枪-AFNetwoking3.0的封装
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51588829 下面是我的新建的Swift学习交流群,欢迎大家一起来共同学习Swift。
867 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等