Swift - where关键字使用详解(额外条件语句)
作者:hangge | 2017-10-23 08:10
where 语句可以用来设置约束条件、限制类型,让代码更加简洁、易读。
(3)与协议结合
注意:过去 if let、guard 语句中的 where 现在都用“,”替代了
(2)由于 associatedtype 支持追加 where 语句,现在 Swift 4 标准库中 Sequence 中 Element 的声明如下:
1,Swift3 中 where 语句的作用
(1)可以使用 where 关键词在 switch、for in 语句上做些条件限制。

let scores = [20,8,59,60,70,80]
//switch语句中使用
scores.forEach {
switch $0{
case let x where x>=60:
print("及格")
default:
print("不及格")
}
}
//for语句中使用
for score in scores where score>=60 {
print("这个是及格的:\(score)")
}

(2)在 do catch 里面使用
enum ExceptionError:Error{
case httpCode(Int)
}
func throwError() throws {
throw ExceptionError.httpCode(500)
}
do{
try throwError()
}catch ExceptionError.httpCode(let httpCode) where httpCode >= 500{
print("server error")
}catch {
print("other error")
}
(3)与协议结合
protocol aProtocol{}
//只给遵守myProtocol协议的UIView添加了拓展
extension aProtocol where Self:UIView{
func getString() -> String{
return "string"
}
}
注意:过去 if let、guard 语句中的 where 现在都用“,”替代了
var optionName: String? = "Hangge"
if let name = optionName , name.hasPrefix("H"){
print("\(name)")
}
2,Swift4 中的改进
(1)可以在 associatedtype 后面声明的类型后追加 where 约束语句
protocol MyProtocol {
associatedtype Element
associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
}
(2)由于 associatedtype 支持追加 where 语句,现在 Swift 4 标准库中 Sequence 中 Element 的声明如下:
protocol Sequence {
associatedtype Element where Self.Element == Self.Iterator.Element
// ...
}
它限定了 Sequence 中 Element 这个类型必须和 Iterator.Element 的类型一致。原先 Swift 3 中到处露脸的 Iterator.Element,现在瘦身成 Element。
比如我们对 Sequence 做个扩展,增加个 sum 方法(计算总和):
extension Sequence where Element: Numeric {
var sum: Element {
var result: Element = 0
for item in self {
result += item
}
return result
}
}
print([1,2,3,4].sum)
同样地,SubSequence 也通过 where 语句的限定,保证了类型正确,避免在使用 Sequence 时做一些不必要的类型判断。
(3)除了 Sequence,Collection 同样新增了 where 语句约束。比如下面样例,我们扩展 Collection 时所需约束变得更少了。
protocol Sequence {
associatedtype SubSequence: Sequence
where SubSequence.SubSequence == SubSequence, SubSequence.Element == Element
}
(3)除了 Sequence,Collection 同样新增了 where 语句约束。比如下面样例,我们扩展 Collection 时所需约束变得更少了。
// 在Swift 3时代, 这种扩展需要很多的约束:
//extension Collection where Iterator.Element: Equatable,
// SubSequence: Sequence,
// SubSequence.Iterator.Element == Iterator.Element
//
// 而在Swift 4, 编译器已经提前知道了上述3个约束中的2个, 因为可以用相关类型的where语句来表达它们.
extension Collection where Element: Equatable {
func prefieIsEqualSuffix(_ n: Int) -> Bool {
let head = prefix(n)
let suff = suffix(n).reversed()
return head.elementsEqual(suff)
}
}
print([1,2,3,4,2,1].prefieIsEqualSuffix(2))
全部评论(0)