Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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" ]


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

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


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"