if else分支结构与三目运算符
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let x = 10
if x > 20 {
print("大了")
}else{
print("小了")
}
x > 20 ? print("大了") : print("小了")
}
}
- 可以看到 if x > 20 这里的小括号()没有了;
- if else的分支中大括号{}必须写上,不能不写;
- x > 20 ? print("大了") : print("小了") 这是三目运算,与OC相比基本没有变化;
- 在Swift中三目运算用的会很多;
if let 指令 可选项绑定
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let name : String? = "李四"
let age : Int? = 20
if name != nil && age != nil{
print("name = " + name! + "--age = " + String(age!))
}
//if let语法 单个变量
if let name1 = name {
print("name = " + name1)
}
//if let语法 多个变量
if let name1 = name,let age1 = age {
print("name = " + name1 + "--age = " + String(age1))
}
}
}
- 定义并初始化了两个可选项常量name和age,在if中判断不为空时,然后再对其强行解包,一定不会造成崩溃;
- if let name1 = name:
if let
语法,将可选项赋值给一个非可选项的常量,在进入分支后,可以保证非可选项的常量一定有值,相比上面的就省略了强制解包的过程;
- if let name1 = name,let age1 = age,针对多个量的用法;
guard指令
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let name : String? = "李四"
let age : Int? = 20
guard let name1 = name else {
print("name1 = nil")
return
}
guard let age1 = age else {
print("age1 = nil")
return
}
//代码能执行到这里 说明name与age都有值
print("name = \(name1) -- age = \(age1)" )
}
}
- guard 与 if let 功能刚好相反;将值为空的逻辑提前执行;
- 在实际开发中,复杂代码一般在条件判断之后,使用guard 会让嵌套层次减少一层!
switch用法
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let number = 10
switch number {
case 10:
print("好")
case 9:
print("良")
default:
print("差")
}
}
}
- 与OC中的相比,每个case内部,执行指令之后,不需要break语句;
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let number = "11"
switch number {
case "10","11":
let name = "liyanyan"
print("\(name) 好")
case "9": print("良")
default: print("差")
}
}
}
- 可以看到在Swift中对于数据的判断不仅仅只局限于整型,可以是任意类型,而在OC中只能是针对整型;
- 各个case之间不会相互穿透,如果有多个值公用一个case的情况,可以使用逗号隔开;
- case "10","11": 在其内部定义变量,可以不使用大括号{},但在OC中必须使用大括号{};
- 在Swift中,
必须保证要处理所有分支的情况
,即每一个case内部 至少包含一条指令,不写会报错,若不写指令也可以直接写break语句;
fallthrough
- fallthrough可以实现case之间的贯穿效果;
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var number = 1
switch number {
case 1:
print("hahaha")
fallthrough
case 2:
print("hehhe")
default:
print("other")
}
}
}
- 由于在case1内部使用fallthrough,具有贯穿效果,所以紧接着会打印case2中的内容;
switch支持Character与String类型
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let name = "liyanyan"
switch name {
case "li":
print("111")
case "yan":
print("222")
case "liyanyan":
print("000")
default:
break
}
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let c : Character = "A"
switch c {
case "A","a":
print("111")
default:
print("222")
break
}
}
}
区间匹配,元组匹配
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let count = 54
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("small")
case 12..<100:
print("big")
default:
print("very big")
}
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let point = (1,1)
switch point {
case (0,0):
print("orgin")
case (_,0):
print("hehe")
case (0,_):
print("haha")
case (-2...2,-2...2):
print("inside")
default:
print("outside")
}
}
}
-
(_,0)
:表示左边的忽略,只比较右边的;
-
(0,_)
:表示左边的忽略,只比较左边的;
- 使用下划线,表示忽略某个值;
- 关于case的匹配问题,属于模式的匹配;
值绑定
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let point = (2,0)
switch point {
case (let x,0):
print("\(x)")
case (0,let y):
print("\(y)")
case let (x,y):
print("\(x),\(y)")
}
}
}
- point = (2,0),然后去分支中去匹配,发现右侧为0,与case (let x,0) 相匹配,然后将2赋值给x;
where
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let point = (1,-1)
switch point {
case let (x,y) where x == y:
print("x = y")
case let (x,y) where x == -y:
print("x = -y")
case let (x,y):
print("\(x) -- \(y)")
}
}
}
-
where x == y
:是判断x是否等于y,最终的执行结果为print("x = -y");
- where也可以使用在for循环中,代码如下:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let nums = [10,20,30,40,-50,-90]
var sum = 0
for num in nums where num > 0 {
sum += num
}
print(sum)
}
}
- 所有的整数相加,最终结果为100;
- where 是检测当前循环是否满足条件,相当于continue;
标签语句
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
outer: for i in 1...4 {
for j in 1...4 {
if j == 3 {
continue outer
}
if i == 3 {
break outer
}
print(" i == \(i) j == \(j)")
}
}
}
}