I am stuck at a problem which i am trying to figure out in Swift 4.
Let say, i have the below variables
let var1 = "One"
let var2 = "Two"
let var3 = "Three"
var counter = 1
// Loop Start
let currentVariable = "var" + "\(counter)"
//Fetch the value of variable stored under currentVariable
counter += 1
//Loop end
I am trying to get the value based on variable name stored under currentVariable
.
You can set up dictionary, replacing your variable names with keys.
The simple function:
I'm pretty sure you can make things a bit more elegant, but you get the idea. Also, keep in mind that a Dictionary is "unordered", as opposed to an array.