在swift语法中:
1,if 要有明确的真假值
if 后面不需要加(),但必须要有明确的Bool值
let age = 20
if age > 18 {
print("大于18岁")
}
在swift中还有一个guard语法 跟if语法很像
func onLine(age : Int) -> Bool{
guard age >= 18 else {
/ /不大于18走这里
print("回家找妈妈")
return false
}
//大于18走下下面这个
print("可以上网")
return true
}
2,switch 基本用法和OC用法一致
不同之处:switch后可以不跟(),case后可以不跟break(默认的会有)
switch sex {
case 0:
print("男")
fallthrough
case 1:
print("女")
default:
print("其它")
}
补充一:case中可以判断多个条件,用,隔开
switch sex {
case 0, 1:
print("正常人")
default:
print("其它")
}
补充二:switch可以判断浮点型
let a = 3.14
switch a {
case 3.14:
print("π")
default:
print("其它")
}
补充三:switch可以判断字符串
let m = 20
let n = 30
let opration = "/"
var result : Int
switch opration {
case "+":
result = m + n
case "-":
result = n - m
case "/":
result = n / m
default:
print("其它")
}
补充四:switch可以判断区间
//开区间:0~10 swift :0...10
//闭区间:0~9 0..<10
let score = 88
switch score {
case 0..<60:
print("不及格")
case 60..<70:
print("及格")
case 70...100:
print("良好")
default:
print("")
}