Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialingavelikanova
4,528 PointsHi I have difficulty with dictionaries in swift ,I am not sure I understand the assignment .it is chalenge task 2
I added code but it doesn't compile
let currencies = [ "US" : "Dollar" , "UK" : "Pound" , "JP" : "Yen" ];
let ukCurrency = ["UK"];
1 Answer
Alvin Abia
Courses Plus Student 23,034 PointsHey Inga, the good news is that the issue I think you're facing is a relatively simple one to fix. When you are creating a dictionary as you did here (I am just spacing your code a little differently for explanation's sake):
let currencies = [
"US" : "Dollar" ,
"UK" : "Pound" ,
"JP" : "Yen"
]
The first strings before the colon ("US", "UK", "JP") are the keys. The second strings after the colon ("Dollar", "Pound", "Yen") are the values/objects you are setting to the respective keys.
If you are trying to access the value/object for a specific key (such as accessing "Pound" above), you would type in the array's name followed by square brackets containing its respective key as a string.
let ukCurrency = currencies["UK"]
This above line of code is basically declaring a constant named ukCurrency that will hold whatever is the value for the key "UK" in the currencies array. Hope this answer helps, let me know how it goes!