It's probably better to use composition for your case.
... but if a custom sort of Array is what you really want, there is actually a weird and not recommended option to hack that.
You can't inherit from Array since it's a struct. But you can implement the Collection protocol.
struct MemoryComponent {}
struct MemoryComponentsArray: Collection {
// typealias all associatedTypes of `Collection` protocol
typealias Element = MemoryComponent
typealias Index = Array<Element>.Index
typealias SubSequence = Array<Element>.SubSequence
typealias Iterator = Array<Element>.Iterator
typealias Indices = Array<Element>.Indices
/// Your real data storage
private let internalArray: Array<Element>
/**
Create any custom initializers you need
*/
init(_ arrayLiteral: [Element]) {
self.internalArray = arrayLiteral
}
// Implement `Collection` protocol core stuff
// By referencing to internal array
var startIndex: Index { internalArray.startIndex }
var endIndex: Index { internalArray.endIndex }
func makeIterator() -> Iterator { internalArray.makeIterator() }
subscript(position: Index) -> Element { internalArray[position] }
subscript(bounds: Range<Index>) -> SubSequence { internalArray[bounds] }
var indices: Indices { internalArray.indices }
var isEmpty: Bool { internalArray.isEmpty }
var count: Int { internalArray.count }
func index(_ i: Index, offsetBy distance: Int) -> Index {
internalArray.index(i, offsetBy: distance)
}
func index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
internalArray.index(i, offsetBy: distance, limitedBy: limit)
}
func distance(from start: Index, to end: Index) -> Int {
internalArray.distance(from: start, to: end)
}
func index(after i: Index) -> Index {
internalArray.index(after: i)
}
func formIndex(after i: inout Index) {
internalArray.formIndex(after: &i)
}
}