How to make a 2D array of strings and dictionaries?

I must be missing something simple. I want to make a 2D array where each "column" has two "rows", a name string and a dictionary. So here's an example with four strings. I want to attach a dictionary to each string. A few things I've tried (in Playgorund) are shown.


var myNames = ["Home", "Accesory", "Service", "Characterstic"]  // My array of name strings
var myDict: [String: Any] = [  // The dictionary I want to attach to each string name
    "addtionalRows" : 1,
    "cellIdentifier" : "idCellNormal",
    "isExpandable" : true,
    "isExpanded" : false,
    "isVisible" : true,
    "primaryTitle" : "",
    "secondaryTitle" : "Fullname",
    "value" : ""
]
var test2DArray = [[String](), [String: Any]()]  // This throws an error
var test2DArray = [[String](), [String: Any]()] as [Any]  // This does not cause an error, but doesn't seem to work
test2DArray = [myNames, myDict]  // Likewise, this does not cause an error but it doesn't fill in the array

for row in HomekitRows {
    test.append(HomekitRows[row], cellProps)  // This fails if the "as [Any]" form above is used
}


What am I missing?


[UPDATE] Rather than mixing types, I decided I could use an array of just dictionaries. I added the key "category" to the myDict declared above and put my name string into it. It works!


var myDictArray: [[String: Any]] = [[:]]
for row in myNames {
myDict.updateValue(row, forKey: "category") // This puts my strings directly into each dictionary
if row == "Service" {
   myDict.updateValue(false, forKey: "isVisible") // This is just a test to verify I can alter dictionary values
}
myDictArray.append(myDict) // This works!!
}

Your first definition cannot work :

var test2DArray = [[String](), [String: Any]()]

it is an array with 2 different types ; hence you get the error : Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional

as the message says, it is understood as an Array of Any ; hence, second line compiles.


You could have defined a struct

struct RowOfArray {
     var name : String
     var dico : [String: Any]
}

And then define you array

var test2DArray = [RowOfArray]()


But if you have found a solution that works, that's great.

How to make a 2D array of strings and dictionaries?
 
 
Q