Cannot use mutating member on immutable value: subscript is get-only


#1
import Cocoa

struct MyData {
    let t: TimeInterval
    let q: Int
}

extension Collection where Index == Int, Element == [MyData] {

mutating func add(_ new: MyData) {
    guard !self.isEmpty else {
        self = [[new]] as! Self
        return
    }
    
    self[self.count - 1].append(new) /// <---- ???
    }
}

var myData: [[MyData]] = []
myData.add(MyData(t: Date().timeIntervalSince1970, q: 1))

print(myData)

Фунция мутирующая, но она почему-то не дает доступ к последнему элементу, чтобы добавить новый в 2д массив.

Еще почему-то self.last больше не работает (xcode 13 beta 3).


#2
extension Array where Element == Array<MyData> {
    mutating func add(_ new: MyData) {
        guard !isEmpty else {
            self = [[new]]
            return
        }
        self[count - 1].append(new)
    }
}