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 trial

iOS

getting info from a dictionary in an array (swift basic)

hello everybody,

i'm learning swift programming from scratch and as a little exercise i was playing with the arrays and dictionaries a bit.

i created 3 dictionaries song1 , song2, song3 i created an array songs with has the dictionaries song1, song2, song3

i can get the string from the array by the following command:

songs[0]

now when i try to get the info from a key in one of the dictionaries with the array index it gives an error:

songs[0["artist"]]

how can i solve this?

full code:

let song1 = ["title": "i don't know much","artist": "aaron neville","album": "love at first sight"]

let song2 = ["title": "all of me","artist": "john legend","album": "legends"]

let song3 = ["title": "afsheid nemen bestaat niet","artist": "marco borsato","album": "marco!"]

let songs = [song1,song2,song3]

songs[0["artist"]]

2 Answers

First, you need to close the brackets for accessing the array before you open the brackets to access the dictionary. Also note that you didn't assign the value you retreived from the array and dictionary above to a const/variable. This should work for you.

let songArtist = songs[1]["artist"]
println(songArtist)

Hope it helped. :)

this solves it!

ty very much for the quick solution stephen