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 Swift Basics (retired) Collections What is a Dictionary?

Osiris Maldonado
Osiris Maldonado
699 Points

Help with expanding on the Extra Credit for Collections: Create a Song Dictionary and Print it out Neatly.

I know that there are different approaches to this challenge, and this is mine, some feedback would be nice:

let firstSong = ["title": "Accept Yourself", "Artist": "The Smiths", "Album": "Complete (Remastered)"]
let secondSong = ["title": "Aeroplane Flies High", "Artist": "Smashing Pumpkins", "Album": "Greatest Hits" ]
let thirdSong = ["title": "Ain't No Sunshine", "Artist": "Fats Domino", "Album": "The Rolling Stone" ]


println("Songs")
println(firstSong["title"])
println(firstSong["Artist"])
println(firstSong["Album"])

As you can see, I also wanted to print the list neatly, in the console I get "Optional" as a return for each element, how can I omit that? Essentially I would love some feedback on my song dictionary (Is it good? Efficient? Is there a better alternative?) and some feedback on the println function for it would be great too.

Thank you!

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

Your song dictionaries look great. No problems there.

However, it looks like you're missing an important element from the original assignment...the array to hold the songs. The array is important to reach some semblance of efficiency - which it looks like you're trying to achieve with your expansion.

Here's an example:

//my code goes after your initial definitions

let songLibrary = [firstSong, secondSong, thirdSong]

println("Songs")   // you already had this as your fist line so I kept it here

for songNum in songLibrary {   // this loop replaces your individual println statements
     println()   // an extra line for neatness
     println(songNum["title"])
     println(songNum["Artist"])
     println(songNum["Album"])
}

Output should look like this (followed by songs two and three):

Songs

Accept Yourself
The Smiths
Complete (Remastered)

The optional part of the console output is something that is covered farther on in the iOS track. Keep going to figure out what's up with that...

Osiris Maldonado
Osiris Maldonado
699 Points

Great reply, thank you! I think there is also a way of assigning the songs and its properties to a single variable without having to create three different ones, right? Something along the lines of:

let musicCollection: [Dictionary<String, String>] = [
    [
        "album": "Zoso",
        "artist": "Led Zeppelin",
        "title": "Black Dog"
    ],
    [
        "album": "Back in Black",
        "artist": "ACDC",
        "title": "Shoot to Thrill"
    ],
    [
        "album": "Self-Titled",
        "artist": "White Snake",
        "title": "Still of the Night"
    ]
]