SWIFT Advanced Mode Matching if Case

share picture

let age = 22
let sex = “girl”

if (sex == “girl” && age >= 18 && age <= 25){
print(“girl”)
} else {
print(“Go you”)
}

A new syntax has been added for us in swift2: if case

if case 18…25 = age {
print(“free”)
} else {
print(“without”)
}

You can add conditions through where

Of course it is not needed, swift2 provides us with the syntax of conditional filtering: where

if case 18…25 = age where sex == “girl”{
print(“Yes, Old Iron”)
} else {
print(“Let’s go”)
}

[swift] if case, guard case, for case
Reference: https:/ /www.jianshu.com/p/dc42bdbbf473

Leave a Comment

Your email address will not be published.