diff --git a/SwiftStudy.playground/Pages/03 流程控制.xcplaygroundpage/Contents.swift b/SwiftStudy.playground/Pages/03 流程控制.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..10bcd21 --- /dev/null +++ b/SwiftStudy.playground/Pages/03 流程控制.xcplaygroundpage/Contents.swift @@ -0,0 +1,190 @@ +//: [Previous](@previous) + +//: ## 区间运算符 + +//: ### 闭区间运算符 +/*: + 闭区间运算符: + a...b : a <= 取值 <= b + */ + +let names = ["Anna", "Alex", "Brian", "Jack"] +for i in 0...3 { + print(names[i]) +} + +let a = 1 +var b = 3 +for i in a...b { + print(names[i]) +} + +let range = 1...3 +for i in range { + print(names[i]) +} + + + +// i默认是let,有时需要声明为var +for var i in 1...3 { + // i的值可以发生变化 + i += 5 + print(i) +} + +// 用不上变量,可以用_进行忽略 +for _ in 1...3 { + print("123456") +} + +//: ### 半开区间运算符 +/*: + 半开区间运算符 + a.. = 1...3 +// 半开区间 Range +let range2: Range = 1..<3 +// 单侧区间 PartialRangeThrough +let range3: PartialRangeThrough = ...5 + + +//: ### 带间隔的区间值 +// 使用函数stride +for tickMark in stride(from: 4, to: 11, by: 2) { + print(tickMark) +} + + +//: ## switch +/*: + switch注意点 + - switch必须保证能处理所有的情况 + - 能处理所有情况的下,可以省略default + - switch 也支持Character、String类型 + - switch 支持区间匹配以及元组匹配 + */ + +// 默认可以不写break,并不会贯穿到后面的条件 +var num = 1 +switch num { +case 1: + print("number is 1") +case 2: + print("number is 2") +default: + print("number is other") +} + +// 使用fallthrough可以实现贯穿效果 +var number = 1 +switch number { +case 1: + print("number is 1") + fallthrough +case 2: + print("number is 2") +default: + print("number is other") +} + +//: #### 区间匹配 +let count = 62 +switch count { +case 0: + print("none") +case 1..<5: + print ("a few") +case 5..<12: + print("several") +case 12..<100: + print("dozens of") +case 100..<1000: + print("hundreds of") +default: + print("many") +} // dozens of + + + + +//: #### 元组匹配 +// 可以使用下划线_ 忽略某个值 +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 box") +default: + print( "outside of the box") +} + +//: ### 值绑定 +// 必要时 let 可以换为 var +let point1 = (2, 0) +switch point1 { +case (let x, 0): + print("on the x-axis with a x value of \(x)") +case (0, let y): + print("on the y-axis with a y value of \(y)") +case let(x, y): + print("somewhere else at (\(x), \(y))") +} + +//: ### where +// 1. 用在case的后边 +let point2 = (1, -1) +switch point2 { +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("somewhere else at (\(x), \(y))") +} + +//2. 对数据进行过虑 +var numbers = [10, 20, 30, -10, -30] +var sum = 0 +for num in numbers where num > 0 { + sum += num; +} +print(sum) + +//: ### 标签语句 +// 标签语句是 +// 标签: 代码 +outer: for i in 1...4 { + for k in 1...4 { + if k == 3 { + continue outer + } + + if i == 3 { + break outer + } + + print("i == \(i), k == \(k)") + } +} + + +//: [Next](@next) diff --git a/SwiftStudy.playground/contents.xcplayground b/SwiftStudy.playground/contents.xcplayground index 96638d2..9e18224 100644 --- a/SwiftStudy.playground/contents.xcplayground +++ b/SwiftStudy.playground/contents.xcplayground @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate index 386bfc0..f575132 100644 Binary files a/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate differ