How to create multidimentional arrays and get/set values at a specific row/column?

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.


Row 1: UUID = 0, Item1 = "John", Item2 = "Jane"
Row 2: UUID = 1, Item1 = "Dennis", Item2 = "Drake"

import SwiftUI
struct ContentView: View {
     private var ListArray =  [[String]]()
    @State private var text: String = ""
    @State private var label: String = ""
    var body: some View {
      VStack {
          Button ("Add row"){
              ListArray.insert([UUID]["John"]["Jane"], at: 0)
              ListArray.insert([UUID]["Dennis"]["Drake"], at: 1)
          Button ("Remove row 1, item 2"){
              ListArray.remove(at: 1, 2)
          Button ("Get Item row1, item 2"){
              label = ListArray [1, 2 ]
          TextEditor (text: $text)
              .frame(width: 150, height: 150, alignment: .leading)
          Text (label)

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

Other thing: name variables starting with lower case (upper cased names are for types).


Thanks a lot for the answer, this is my first question, I'm not quite sure if I have a way to accept your answer etc or upvote you?

Ah, just like it with the heart button (as you just did).

As per SwiftUI or other Apple SDK related questions, stackoverflow is good enough for now until Apple fixed their forums.

1 Like

Yeah, true.

And another question, to remove an item by UUID, is the following is alright or is there any other method?

if let index = listArray.firstIndex(of: "UUIDString") {
    listArray.remove(at: index)

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 { $ == 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 == uuid

  // array of array of strings case:
  listArray.removeAll { item in
      item[0] == uuidString

Thanks a lot