有没有办法使用键路径表达式来简化 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 中表达,使用\.bool
on struct Struct
,而不诉诸于structs.filter(\.bool).count > 0
?