How to subclass an array class in Swift?

Swift's Array type is a structure, and in Swift, base classes must be actual classes (that is, class Foo) and not structures.

So you cannot do what you are trying to do via inheritance from Array, unfortunately. You could, however, store the array as a field within your class and forward methods to it, possibly implementing any protocols you want to support, et cetera.


In Swift, Array is a struct, not a class. To have a class that is an array subclass, you will need to use NSArray, its Objective-C counterpart.

For example,

class Foo: NSArray{}

In Swift 2.x you can use a protocol extension.

class Foo : Equatable {}
// you need to provide the Equatable functionality
func ==(leftFoo: Foo, rightFoo: Foo) -> Bool {
    return ObjectIdentifier(leftFoo) == ObjectIdentifier(rightFoo)
}

extension Array where Element : Foo {}

protocol extensions provide "insert points" to extend classes that aren't classes, classes you don't own, etc.