使用where語(yǔ)句之前必須在where之前有一個(gè)主語(yǔ)變量名
- 協(xié)議約束
//基類A繼承了SomeProtocol協(xié)議才能添加擴(kuò)展
extension SomeProtocol where Self: A {
func showParamA() {
print(self.a)
}
}
- for...in...遍歷
let arr = [1, 2, 4, 5, 6]
for (index, value) in arr.enumerated() where value > 4 {
print(index, value)
}
- case 語(yǔ)句枕扫,相當(dāng)于if判斷
let arr = [1, 2, 4, 5, 6]
for (index, value) in arr.enumerated() where value > 4 {
switch value {
case let a where a < 6:
print(index)
default:
print(value)
}
}
- if let 和 guard中,在swift4.0版本以后劲藐,使用逗號(hào)代替where
let str : String? = "hello"
if let value = str, value.count == 5 {
print(value)
}
guard let value = str, value.count == 5 else { return }
print(value)