Is it possible in Swift to have interpolation of property (variable) names from the content of other properties? In the code segment below, In the first for loop I would like to set the property eachItem to the item name contained in the rotation array. Then in the second inner for loop I want to iterate through the array named by what is in the eachItem property.
var groupA = ["groupA1", "groupA2", "groupA3"]
var groupB = ["groupB1", "groupB2", "groupB3", "groupB4", "groupB5", "groupB6"]
var groupC = ["groupC1", "groupC2", "groupC3", "groupC4", "groupC5"]
var rotation = ["groupA", "groupB", "groupC"]
for eachGroup in rotation {
for eachItem in \(eachGroup) {
print(eachItem)
}
}
The iterations through the two for loops would give us:
eachGroup = "groupA"
eachItem = "groupA1"
eachItem = "groupA2"
eachItem = "groupA3"
eachGroup = "groupB"
eachItem = "groupB1"
eachItem = "groupB2"
eachItem = "groupB3"
eachItem = "groupB4"
eachItem = "groupB5"
eachItem = "groupB6"
eachGroup = "groupC"
eachItem = "groupC1"
eachItem = "groupC2"
eachItem = "groupC3"
eachItem = "groupC4"
eachItem = "groupC5"
Is variable/property name interpolation possible in Swift? If so, how can I do that?
Additionally, is it better to handle this using a two-dimensional array? If so, how would I do that?