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 trialKenton Hubner
6,314 PointsSwift extra credit challenge, dictionary
I wasn't sure what was exactly meant so I have two examples to see if either one was correct.
import UIKit
var songList = ["Song One":"Abby Dabby",
"Song Two":"Abby Dabby",
"Song Three":"Abby Dabby",
"Song Four":"Abby Dabby",
"Song Five":"Abby Dabby",
"Song Six":"Abby Dabby",
"Song Seven":"Abby Dabby",
"Song Eight":"Abby Dabby",
"Song Nine":"Abby Dabby",
"Song Ten":"Abby Dabby",]
// OR
let songOne = ["title" : "Back in Black", "Artist":"Metallica", "Album":"Neverland"]
let songTwo = ["title" : "Back in Red", "Artist":"Metallica", "Album":"Neverland"]
let songThree = ["title" : "Back in Blue", "Artist":"Metallica", "Album":"Neverland"]
let songFour = ["title" : "Back in Yellow", "Artist":"Metallica", "Album":"Neverland"]
let songFive = ["title" : "Back in Green", "Artist":"Metallica", "Album":"Neverland"]
let songSix = ["title" : "Back in Pink", "Artist":"Metallica", "Album":"Neverland"]
let songSeven = ["title" : "Back in White", "Artist":"Metallica", "Album":"Neverland"]
let songEight = ["title" : "Back in Orange", "Artist":"Metallica", "Album":"Neverland"]
let songNine = ["title" : "Back in Silver", "Artist":"Metallica", "Album":"Neverland"]
let songTen = ["title" : "Back in Brown", "Artist":"Metallica", "Album":"Neverland"]
var songArray = [songOne, songTwo, songThree, songFour, songFive, songSix, songSeven, songEight, songNine, songTen]
4 Answers
Chris Shaw
26,676 PointsHi Kenton,
Your second example is pretty much there apart of the key names, however to make it more explicit what the array contains I feel the below is what the extra credit is aiming for because dictionaries in Swift are explicit types so they can only carry one type based on the type assigned to it.
var songs: [Dictionary<String, String>] = [
[
"title": "Back in Black",
"artist": "Metallica",
"album": "Neverland"
],
[
"title": "Back in Red",
"artist": "Metallica",
"album": "Neverland"
]
// ...
]
Personally this is how I would do it but the type assignment [Dictionary<String, String>]
isn't required because Swift will infer the type during compilation but it's best to help it out wherever possible.
Kenton Hubner
6,314 PointsThanks! I didn't know I could string them together like that.
Meg Cusack
11,448 PointsThanks, this was very helpful. Does Treehouse actually post the answers to their extra credits somewhere? It would be nice to see it.
David Lett
7,483 PointsSo can you do this without the [Dictionary<String, String>] and just start from the first array? Also how would you call each song if you wanted to display the results?
michael de marigny
3,411 PointsHi Chris
Thank you for your example to the solution. My question is how would you add to the dictionary using inserts, appends and removes? Would you be able to provide some samples?
thanks
David kidd
1,684 PointsDavid kidd
1,684 PointsThats a very elegant way to do this. Thanks for sharing. I did it like the OP's second method as i wasnt aware it could be completed this way.