跳到主要内容

Swift 中的 where 用法

· 阅读需 2 分钟

switch里面的用法

假设有个enum

enum Action {
case createUser(age: Int)
case createPost
case logout
}

我们可以用where 来过滤特殊年龄的情况:

func printAction(action: Action) {
switch action {
case .createUser(let age) where age < 21:

print("Young and wild!")
case .createUser:
print("Older and wise!")
case .createPost:
print("Creating a post")
case .logout:
print("Logout")
}
}

printAction(action: Action.createUser(age: 18)) // Young and wild
printAction(action: Action.createUser(age: 25)) // Older and wise

for循环里的用法

打印偶数数字:

let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers where number % 2 == 0 {
print(number) // 0, 2, 4, 6, 8, 10
}

协议扩展里面的用法

基于元素类型来扩展Array

extension Array where Element == Int {

func printAverageAge() {
let total = reduce(0, +)
let average = total / count
print("Average age is \(average)")
}
}

let ages = [20, 26, 40, 60, 84]
ages.printAverageAge() // Average age is 46

集合方法first里的用法

根据条件读取第一个元素

let names = ["Henk", "John", "Jack"]
let firstJname = names.first(where: { (name) -> Bool in
return name.first == "J"
}) // Returns John

集合方法contains里的用法

根据条件判断是否包含某个元素

let fruits = ["Banana", "Apple", "Kiwi"]
let containsBanana = fruits.contains(where: { (fruit) in
return fruit == "Banana"
}) // Returns true

初始化里的用法

只允许特定类型的初始化

extension String {
init(collection: T) where T.Element == String {
self = collection.joined(separator: ",")
}
}

let clubs = String(collection: ["AJAX", "Barcelona", "PSG"])
print(clubs) // prints "AJAX, Barcelona, PSG"