concept Array in category swift

appears as: Array, n Array
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

Another generic type is Array. You write them as [Int] or [String] or something similar, which is syntactic sugar. Secretly, the syntax is Array<Element>, such as Array<Int>, which also compiles.

13.2.3. Making Array conditionally conform to a custom protocol

Since Swift 4.1, you can solve this problem where Array will conform to a custom protocol. You can make Array conform to Track, but only if its elements conform to Track. The only difference from before is that you add : Track after Array.

Listing 13.16. Making Array conform
// Before. Not conditionally conforming.
extension Array where Element: Track {
    // ... snip
}

// After. You have conditional conformance.
extension Array: Track where Element: Track {
    func play() {
        for element in self {
            element.play()
        }
    }
}

Now Array is a true Track type. You can pass it to functions expecting a Track, or nest arrays with other data and you can still call play() on it, as shown here.