在switch case中,每个case分支的statements不需要用{ }包含,例如
(1)
let oneSring: String = "abc"
switch oneSring {
case "abc":
{
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
}
case "A":
print("The String is A")
default:
print("Not find String")
}
在第一个case分支,会报错:
(2)每个case分支如果有多条语句,直接换行写就可以了,
let oneSring: String = "abc"
switch oneSring {
case "abc":
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
case "A":
print("The String is A")
default:
print("Not find String")
}
输出结果为: