swift 高级模式匹配 if case

let age = 22
let sex = "girl"

if (sex == "girl" && age >= 18 && age <= 25){
print("女朋友")
} else {
print("走你")
}

swift2中为我们增加了一种新的语法: if case

if case 18...25 = age {
print("免费")
} else {
print("不带")
}

可以再通过where去添加条件

当然是不需要的,swift2为我们提供了条件筛选的语法: where

if case 18...25 = age where sex == "girl"{
print("可以的,老铁")
} else {
print("走吧")
}

【swift】if case,guard case,for case
参考: https://www.jianshu.com/p/dc42bdbbf473
参考:(if let 、guard let)https://www.jianshu.com/p/e1fe08c5db1a

原文地址:https://www.cnblogs.com/sanjianghuiliu/p/11229308.html