//(1)var str = "abc"if(str == nil){
}
//String? 为String可选型,他意味着这种类型,可以为nil,还是那句话,可选型和nil不是一个类型的特殊状态,他是一种新的类型。//2var optionalStr:String? = "hello"
optionalStr = "world"print(optionalStr == nil)
var greet = "hey"if let name = optionalStr{
greet = "hello \(name)";
}
print(greet)
if(optionalStr != nil){
print("optional str is not nil")
}
where与模式匹配
//switch中使用where语句let point = (3,3)
switch point {
caselet(x,y) where x == y:
print("x == y")
caselet(x,y) where x == -y:
print("x == -y")
default:
print("rst is \(point.0) , \(point.1)")
}
//switch中使用运算符 let age = 19switch age{
case10 ... 19:
print("teenager")
default:
print("not teenager")
}
//if中使用模式 ifcase10...19 = age where age > 18{
print("teenager and in colldge")
}
//if中使用模式和where语句let vector = (4,0)
ifcase (let x,0) = vector where x > 2 && x < 5{
print("it is vector")
}
//if中使用模式+where+运算符 forcaselet i in1 ... 100where i % 3==0{
print("i is \(i)")
}