Extending typed Arrays (of primitive types like Bool) in Swift 3?
Just extend the Collection or the Sequence
extension Collection where Element == Bool {
var allTrue: Bool { return !contains(false) }
}
edit/update:
Xcode 10 • Swift 4 or later
You can use Swift 4 or later collection method allSatisfy
let alltrue = [true, true,true, true,true, true].allSatisfy{$0} // true
let allfalse = [false, false,false, false,false, false].allSatisfy{!$0} // true
extension Collection where Element == Bool {
var allTrue: Bool { return allSatisfy{ $0 } }
var allFalse: Bool { return allSatisfy{ !$0 } }
}
Testing Playground:
[true, true, true, true, true, true].allTrue // true
[false, false, false, false, false, false].allFalse // true
As of Swift 3.1 (included in Xcode 8.3), you can now extend a type with a concrete constraint:
extension Array where Element == Bool {
var allTrue: Bool {
return !contains(false)
}
}
You can also extend Collection
instead of Array
, but you'll need to constrain Iterator.Element
, not just Element
.
Apple replaced _ArrayType
with _ArrayProtocol
in Swift 3.0 (see Apple's Swift source code on GitHub) so you can do the same thing you did in Swift 2.2 by doing the following:
extension _ArrayProtocol where Iterator.Element == Bool {
var allTrue : Bool { return !self.contains(false) }
}