I am trying to optimize my code. I want to do the following: if I have an element in the dictionary dict that is in firstNames array I want to write this element directly to the first variable, same with second and etc. Here my code example. I've created names array that contains linked variables firstNames <-> first, secondNames <-> second and etc.
Also I am trying to iterate through the loop and to set values to first, second and etc. using val
let dict = ["type": "1", "word": "abc"] // this is example
let firstNames = ["1", "2"]
let secondNames = ["3", "4"]
var first = String()
var second = String()
let names = [firstNames: first, secondNames: second]
for el in dict {
for var (key, val) in names {
if (key as! [String]).contains(el["type"]!) {
if ((val as! String) != "") {
val = (val as! String) + ", " + el["word"]!
}
else {
val = el["word"]!
}
}
}
}
This code doesn't have any runtime errors. But unfortunately when I am trying to set val in the loop nothing is happening. What I am doing wrong or may be there is a more elegant solution?
Thanks for any help.
dictlooks like? Right now yournamesvariable is a dictionary with name as array and value is string ([[String],String]).1abcthen write to1abcd, if I have2abcthen write to2abcd, if I have3abcthen write to3abcd... I mean that how to get name of variable 2 from the same name (partially the same) of variable 1), how will I write to the variable then? If I usesecondDict["variable"] = "smth"` I will just update a dictionary, but I need to updatevariable.