Search code examples
swiftdictionarysubscript

Swift: access to the content of a dictionary


the codes simple:

var dictionnary: [Int: Int] = [25: 32]
var test = "dictionary"

print (dictionary[25])
print (test[25])

I want to access to the content the dictionary like in the last line of code. I have to detect the name of the dictionary i have to use. So I am obliged to store the name of the dictionary in a variable of type String. a get this error :

error i get with last line of code

how can I proceed?

Ok, I tried to make a simple example to explain my problem and I made a mistake. My problem is not related to converting string to a variable name or something else.

In fact I have 10 dictionaries with data in each. I need to identify the good one in order to grab my data.

I obtained a variable of type string containing the name of the good dictionary but when I try to use it to access to the content of this dictionary I obtain the error mentioned.

I will try to explain this correctly when I will be able to post again...


Solution

  • I think you want

    var test = dictionary
    

    Remove the quotations