This isn't a 2-dimensional array. It's an array of arrays. Any previous experience you've had with pseudo-2-dimensional arrays in (for example) C doesn't apply here.
You could define your own type that allows 2 subscripts (so you'd be able to write accesses like array[i, j], but you'd have to come up with your own implementation that figures out how to store the elements. You could even provide an implementation like C's, where you require all the array dimensions except one to be constant, then actually store the elements in a single-dimension Array. Or, you could provide the storage as an array of arrays, but you'd need to monitor the number of elements in each sub-array.
You'd also have to decide on the semantics of operations like "append". Which dimensions can be appended to?
Deciding between these various alternatives and options is going to depend on a clearer statement of what you need to do with these "2 dimensional" arrays.
If you're okay with an Array-Of-Arrays, what you could do is write an extension to handle the complications when there isn't an array at the index you're wanting to append:
()
extension Array {
/// This is the quick and easy version, if this is production code it should be more robust
mutating func append<T>(_ element: T, toIndex index: Index) where Element == Array<T> {
if index >= count {
// Adds empty arrays when
(0...(index - count)).forEach { _ in
append(Array<T>())
}
}
self[index].append(element)
}
}
var matches: [[Matches]] = []
let newMatches = Matches(name: "simon", handicap: 2, displayName: "FD")
matches.append(newMatches, toIndex: 0)
print(matches[0][0].name) // prints Optional("simon")
This doesn't really fix your issue. And if it does, it is very fragile and can easily lead to bugs.
If you call this function in any other order, it will fail. If row != 0 and matches contains data, and you append the new row, it'll end up at the end of your outer array, and not at the zeroth position.
If you populate your array inside a for-loop, you should change it to a map instead. And if you populate your array imperatively from some function, you should create some kind of two-dimensional data structure that stores sparsely populated data using a custom subscript, methinks.