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 Extra Credit

Did anyone try to do the extra credit on the Collections track for swift? (Array of 10 songs where each song is a dictionary?) Would you be willing to share your results?

3 Answers

Steven's answer works, but it isn't necessary to assign each song to a variable, because we aren't using those variables for anything other than to create our array, and when we want to access the songs we will access them through the array and not through the variables songInfo0, songInfo1, etc.

The variables songInfo0 and so forth are dictionaries of type [String: String] that were created using a dictionary literal. The songArray constant in that code is an array of dictionaries of type [String: String]. If we wanted to specify the type of the array it would be written as follows:

let songArray : [ [String: String] ] = // array literal written out here...

Since we are writing out the data in code as an array literal we don't have to specify the type because it can be inferred from the data we provide. Since the array is made up of dictionary literals, and we don't need those dictionaries to be assigned to a variable outside of the array, we can type the dictionary literals inside the array literal without assigning them to a variable first. It is the same as if the array were an array of Strings; we wouldn't need to assign the strings to a variable before adding them to the array, we would just add them to the array as String literals like this:

let someArray = ["one", "two", "three"]

If you want to create an array of dictionaries using an array literal containing dictionary literals instead of creating it using an array literal with a list of variables containing dictionaries it would look like this:

let songArray = [
    [   "title":    "Scholarships",
        "artist":   "Drake and Future",
        "album":    "What a Time to Be Alive"
    ],
    [   "title":    "Stolen Dance",
        "artist":   "Milky Chance",
        "album":    "Sadnecessary"
    ],
    [   "title":    "Know Yourself",
        "artist":   "Drake",
        "album":    "If you're reading this you're too late"
    ],
    [   "title":    "Cream on Chrome",
        "artist":   "Ratatat",
        "album":    "Single"
    ],
    [   "title":   "The Hills",
        "artist":   "The Weeknd",
        "album":    "Single"
    ]
]

Hey Thomas Gallagher,

Thanks for refactoring my code!!! I just posted my original solution to the extra credit. When going back, I should've looked to see if there was a more efficient way of doing it.

Hey Mollie Roberts,

// First I created the dictionaries for my songs

var songInfo0 = [ 
   "title":    "Scholarships",
   "artist":   "Drake and Future",
   "album":    "What a Time to Be Alive"
 ]

var songInfo1 = [
   "title":   "Stolen Dance",
   "artist":   "Milky Chance",
   "album":    "Sadnecessary" 
]

var songInfo2 = [ 
   "title":   "Know Yourself",
   "artist":   "Drake",
   "album":    "If you're reading this you're too late" 
]

var songInfo3 = [
   "title":   "Cream on Chrome",
   "artist":   "Ratatat",
   "album":    "Single" 
]

var songInfo4 = [ 
   "title":   "The Hills",
   "artist":   "The Weeknd",
   "album":    "Single" 
]

// Then I set those dictionaries into an array named "songArray"

let songArray = [(songInfo0), (songInfo1), (songInfo2), (songInfo3), (songInfo4)]

The extra credit says do this for 10 songs but I only posted 5 here.

Good luck!

Hi Steven and Thomas,

Thank you both so much for your answers! It is clear to me now where I went wrong, and I really appreciate your help!

Mollie