1、一般用法
swift语言的switch中自带break,所以使用时可以省略不写
swift语言中case必须包含所有情况,否则必须有default关键字
// if - else
let rating = "A"
if rating == "A"{
print("Great!")
}
else if rating == "B"{
print("Just so-so")
}
else if rating == "C"{
print("It's Bad")
}
else{
print("Error")
}
// switch
switch rating{
case "A":
print("Great!")
case "B":
print("Just so-so")
case "C":
print("It's Bad")
default:
print("Error")
}
2、在一个case中判断多个值
// 在一个case中判断多个值
switch rating{
case "a","A":
print("Great!")
case "b","B":
print("Just so-so")
case "c","C":
print("It's Bad")
default:
print("Error")
}
3、使用switch判断其他类型
别的语言switch只能判断整形,swift能判断所有类型
// 使用switch判断浮点数
let x = 2.8
switch x{
case 2.8:
print("I'm 2.8")
default:
print("I'm not 2.8")
}
// 使用switch判断布尔值
let y = true
switch y{
case true:
print("I'm true")
default:
print("I'm false")
}
4、case后面可以写一个范围
let score = 90
switch score{
case 0:
print("You got an egg!")
case 1..<60:
print("You failed.")
case 60:
print("Just passed")
case 61..<80:
print("Just so-so")
case 80..<90:
print("Good")
case 90..<100:
print("Great!")
case 100:
print("Perfect!")
default:
print("Error score.")
}
5、switch与元组的结合使用
let vector = (1,1)
switch vector{
case (0,0):
print("It's origin!")
case (1,0):
print("It an unit vector on the positive x-axis.")
case (-1,0):
print("It an unit vector on the negative x-axis.")
case (0,1):
print("It an unit vector on the positive y-axis.")
case (0,-1):
print("It an unit vector on the negative y-axis.")
default:
print("It's just an ordinary vector.")
}
//与元组解包结合使用
let point = (1,1)
switch point{
case (0,0):
print("It's origin!")
case (_,0):
print("It on the x-axis.")
case (0,_):
print("It on the y-axis.")
default:
print("It's just an ordinary point.")
}
switch point{
case (0,0):
print("It's origin!")
case (_,0):
print("It on the x-axis.")
case (0,_):
print("It on the y-axis.")
case (-2...2,-2...2):
print("It's near the origin.")
default:
print("It's just an ordinary point.")
}
// Value binding
switch point{
case (0,0):
print("It's origin!")
case (let x,0):
print("It on the x-axis.")
print("The x value is \(x)")
case (0,let y):
print("It on the y-axis.")
print("The y value is \(y)")
case (let x,let y):
print("It's just an ordinary point.")
print("The point is ( \(x) , \(y) )")
}
6、fallthrough的使用
fallthrough可以取消break,是程序语句执行下去
注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
switch point{
case (0,0):
print("It's origin!")
fallthrough
case (_,0):
print("It's on the x-axis.")
fallthrough
case (0,_):
print("It's on the y-axis.")
fallthrough
case (-2...2,-2...2):
print("It's near the origin.")
fallthrough
default:
print("It's just an ordinary point.")
}
/**
It's origin!
It's on the x-axis.
It's on the y-axis.
It's near the origin.
It's just an ordinary point.
*/
// 注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
// 所以,首先,fallthrough不能跳转到一个有逻辑的case中
// 下面的程序报错
//let num = 3
//switch num{
//case -3…3:
// print(“small number”)
// fallthrough
//case let x where x%2 == 1:
// print(“odd number”)
//}
// 其次,不要用switch和fallthrough组合复杂的判断逻辑
// fallthrough应该用于从一般到特殊的判定
// 以下代码的输出,就是错误的。
let num = 3
switch num{
case -3...3:
print("small number")
fallthrough
case 0:
print("zero")
fallthrough
case -100..<0:
print("negative number")
fallthrough
case 1...100:
print("positive number")
fallthrough
default:
print("a number")
}
/**
small number
zero
negative number
positive number
a number
*/