跳转至

流程控制

区间运算符

闭区间运算符 a...b

var range1: ClosedRange<Int> = 1...3
range1 = 2...4
for i in range1 {
  print(i)
}

半开区间运算符 a..<b

let range2: Range<Int> = 1..<3
for i in range2 {
  print(i)
}

单侧区间

var range3: PartialRangeThrough<Int> = ...5
for i in range3 {
  print(i)
}
let stringRange = "a"..."f"
print(stringRange.contains("d"))//true
print(stringRange.contains("h"))//false

设置区间间隔

///可以分别设置累加值和终止值
///终止值有两种,如果是through,则包含最后一个值,如果是to则不包含
let hours = 10
let hourInterval = 2
//tickMark的取值:从4开始,累加2,到达10结束,包含10
for tickMark in stride(from: 4, through: hours, by: hourInterval){
  print(tickMark)
}//4,6,8,10

//tickMark的取值:从4开始,累加2,到达10结束,不包含10
for tickMark in stride(from: 4, to: hours, by: hourInterval){
  print(tickMark)
}//4,6,8

for in

print("1、简单循环获取i值(变量)")
for var i in 1...3 {
  i += 5
  print(i)
}

print("2、循环获取数组值")
let name = ["A","B","C"]
for i in 0...2 {
  print(name[i])
}

print("3、可以将区间设置为变量来使用")
let range = 0...2
for i in range {
  print(name[i])
}

print("4、也可以用变量分开设置区间的左右范围")
let a = 1
let b = 2
for i in a...b {
  print(name[i])
}

print("5、不获取变量值")
for _ in 1...3 {//如果不需要获取循环的变量值,可以使用_来表示,这样就可以节省栈空间
  print("WY")
}

print("半开区间")
for i in 1..<5 {
  print(i)
}

print("直接获取数组中的数据")
let names = ["A","B","C"]
for name in names[0...2] {
  print(name)
}

print("单侧区间")
for name in names[1...] {
  print(name)//B C
}

switch

case、default后面不能写大括号{}。

case、default后面至少要有一条语句,如果不想做任何事情,就可以加个break。

默认已经有break,所以可以显式的写break,不写也不会贯穿到后面条件。

如果我们想要实现贯穿效果,需要加上fallthrough

switch也支持Character、String类型

switch必须要能够实现处理所有的情况,其他情况可以放在default,如果已经确定已经包含所有情况,是可以不写default的

//1、基础写法
let number = 1
switch number {
  case 1:
  print("number is 1")
  case 2:
  print("number is 2")
  default:
  print("number is other")
}//number is 1

//2、fallthrouth
switch number {
  case 1:
  print("number is 1")
  fallthrough
  case 2:
  print("number is 2")
  default:
  print("number is other")
}//number is 1    number is 2

///支持Character、String类型
///组合匹配。中间用,分割
let str = "WY"
switch str {
  case "A":
  print("A")
  case "B":
  print("B")
  case "C","WY":
  print("C or WY")
  default:
  break
}//C or WY

// MARK: 区间匹配
let count = 60
switch count {
  case 0:
  print("none")
  case 1..<60:
  print("a few")
  case 10...100:
  print("dozens of")
  default:
  print("other")
}//dozens of

// MARK: 元组匹配
///判断的类型也可以是元组,元组是否在这个区间内
///可以使用下划线_忽略这个值
let point = (1,1)
switch point {
  case (0,0):
  print("the origin")
  case (_,0):
  print("on the x-axis")
  case (0,_):
  print("on the y-axis")
  case (-2...2,-2...2):
  print("inside the 2*2 box")
  default:
  print("outside of the box")
}//inside the 2*2 box

// MARK: 值绑定
//此处我们在匹配元组时,只匹配了其中一个值,另一个值是用来获取的。
let point2 = (2,0)
switch point2 {
  case (let x,0):
  print("on the x-axis with an x value of X \(x)")
  case (0,var y):
  y += 2
  print("on the x-axis with an x value of Y \(y)")
  default:
  break
}// on the x-axis with an x value of 2

where

where过滤(增加判断条件)

let numbers = [10,20,-11,-22]
var sum = 0
for num in numbers where num > 0 {
  sum += num
}
print(sum)//30

let point3 = (1,-1)
switch point3 {
  case let (x,y) where x == y:
  print("on the line x == y")
  case let (x,y) where x == -y:
  print("on the line x == -y")
  case let (x,y):
  print("other")
}//on the line x == -y

Guard

主要用于提前退出

当guard语句的条件为false时,就会执行else大括号里面的代码 当guard语句的条件为true时,就会跳过guard语句

func test(v: Bool) {
  guard v else {
    print("当条件为false时进入")
    return
  }
  print("当条件为true时进入")
}
test(v: true)
test(v: false)