I want to create multi-dimensional array and the code is messed up. How to get/set values at a specific row/column? I have following data to feed and retrieve.
Ignoring SwiftUI aspects of this question (which are prohibited here) this is the way to work with a multidimensional array:
listArray.insert([UUID().uuidString, "John", "Jane"], at: 0)
or just
listArray.append([UUID().uuidString, "John", "Jane"]) // appends to end
label = listArray[1][2] // get's second element of first row
listArray.remove(at: 0) // removes 0's element
listArray.remove(at: 0) // removes the other element (which is now 0's)
listArray.removeAll() // or just remove all
But perhaps you don't even need a multidimensional array, but an array of structs instead:
struct Row {
let id: UUID
let name: String
let lastName: String
}
var rows: [Row] = [:]
rows.append(Row(id: UUID(), name: "John", lastName: "Smith")]
let lastName = rows[1].lastName
rows.removeAll()
Other thing: name variables starting with lower case (upper cased names are for types).
That's good but only if your listArray is "array of strings" (which was not the case in your original example: it had "array of array of strings"). Something like this will work for you:
// array of structs case:
if let index = (listArray.firstIndex { $0.id == uuid }) {
listArray.remove(at: index)
}
// array of array of strings case:
if let index = (listArray.firstIndex { $0[0] == uuidString }) {
listArray.remove(at: index)
}
or this (simpler):
// array of structs case:
listArray.removeAll { item in
item.id == uuid
}
// array of array of strings case:
listArray.removeAll { item in
item[0] == uuidString
}