【发布时间】:2021-12-11 02:31:41
【问题描述】:
有没有办法使用键路径表达式来简化 Swift contains(where:) 中的样板代码?
例如
struct Struct {
let bool: Bool
}
let structs = [
Struct(bool: false),
Struct(bool: false),
Struct(bool: true),
Struct(bool: false),
Struct(bool: false)
]
let hasTruth = structs.contains { $0.bool }
print(hasTruth) // true
上面的例子是否可以用 Swift 表达,在 struct Struct 上使用 \.bool,而不使用 structs.filter(\.bool).count > 0?
【问题讨论】:
-
structs.contains(where: \.bool)???
标签: swift filter functional-programming contains swift-keypath