Swift4.2からは allSatisfy
メソッドが提供されています
ドキュメント: https://developer.apple.com/documentation/swift/array/2994715-allsatisfy
例:
struct Item {
var id: String
var hasStock: Bool
}
let category1 = [
Item(id: "1", hasStock: true),
Item(id: "2", hasStock: false),
Item(id: "3", hasStock: true)
]
let category2 = [
Item(id: "1", hasStock: false),
Item(id: "2", hasStock: false),
Item(id: "3", hasStock: false)
]
print(category1.allSatisfy { $0.hasStock == false }) // -> false
print(category2.allSatisfy { $0.hasStock == false }) // -> true
// 上2つは次の2つと同じ意味だが、allSatisfyメソッドの方がわかりやすい
print(!category1.contains(where: { $0.hasStock == true })) // -> false
print(!category2.contains(where: { $0.hasStock == true })) // -> true