Reputation: 3
newbie programmer here, can't figure out why it does it...
let a1 = "This is some text"
let x : Int = 1
var stringValue = "a\(x)"
print(stringValue)
I want it to print "This is some text"
, but it only ever prints a1
.
Upvotes: 0
Views: 48
Reputation: 3753
Rather than storing 'This is some text' in a1 string, save it in a dictionary so you can create the key (x1,x2 etc) to access it:
let stringDictionary = ["a1": "This is some text", "a2": "This is more text"]
You can then pull it out using:
let x : Int = 1
let stringKey = "a\(x)"
let stringValue = stringDictionary[stringKey]
print(stringValue)
Upvotes: 1
Reputation: 119031
What you want is an eval or evaluate type function, but that doesn't exist. What you're actually getting is a description of the x
variable, which is the number 1 converted into text, hence a1
.
If you want to get a value for a key you should use a dictionary.
Upvotes: 0