Extending typed Arrays (of primitive types like Bool) in Swift 3?

后端 未结 4 2095
别那么骄傲
别那么骄傲 2021-02-07 20:57

Previously in Swift 2.2 I\'m able to do:

extension _ArrayType where Generator.Element == Bool{
    var allTrue : Bool{
        return !self.contains(false)
    }         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 21:25

    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.

提交回复
热议问题