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
Jake Webb
13,028 PointsExtra Credit: Create an array of 10 songs.
Here is the Extra Credit Task: Create an array of 10 songs where each song is a dictionary. The dictionary contains the following keys: title, artist and album.
I'm confused because I thought a dictionary was an array (just with keys and values). I'm not sure how you would include 10 dictionaries into a single array.
Here, I've created three dictionaries that contain the keys specified in the task above.
var runners = [ "title": "Runners", "artist": "Lecrae", "album": "Anomaly"]
var nuthin = [ "title": "Nuthin", "artist": "Lecrae", "album": "Anomaly"]
var wish = [ "title": "Wish", "artist": "Lecrae", "album": "Anomaly"]
Would someone be able to point me in the right direction? How do I create an array containing 10 dictionaries? Please help lead me to the answer without explicitly telling me the answer.
Thanks!
-Jake
6 Answers
Michael Liendo
15,326 PointsYou're definitely on the right track! The difference between the two collection types is that an array contains an ordered list of values and each value can be accessed by it's index, while a dictionary is unordered list of values, where each value is accessed by it's "key:value" pair.
To point you in the right direction, I would just say remember that an array can hold any type of data type, not just strings and numbers.
Jake Webb
13,028 PointsHi Michael,
Thanks for explaining the difference and pointing me in the right direction. I forgot that an array is just a "box" containing data and that I shouldn't be limited to what I can put in there.
Would you do me a favor and check my work below? I believe this meets the Extra Credit requirements. Note: I tested this in a playground and it seems to be functioning correctly without errors.
I guess what I'm looking for is whether or not the naming of my variables are good. Would someone be able to take a look at my code and know that I was trying to accomplish?
var song1 = ["title": "Outsiders", "artist": "Lecrae", "album": "Anomaly"]
var song2 = ["title": "Welcome to America", "artist": "Lecrae", "album": "Anomaly"]
var song3 = ["title": "Nuthin", "artist": "Lecrae", "album": "Anomaly"]
var song4 = ["title": "Fear", "artist": "Lecrae", "album": "Anomaly"]
var song5 = ["title": "Anomaly", "artist": "Lecrae", "album": "Anomaly"]
var song6 = ["title": "Timepiece", "artist": "Lecrae", "album": "Anomaly"]
var song7 = ["title": "Dirty Water", "artist": "Lecrae", "album": "Anomaly"]
var song8 = ["title": "Wish", "artist": "Lecrae", "album": "Anomaly"]
var song9 = ["title": "Runners", "artist": "Lecrae", "album": "Anomaly"]
var song10 = ["title": "All I Need Is You", "artist": "Lecrae", "album": "Anomaly"]
var myFavoriteAnomalySongs = [song1, song2, song3, song4, song5, song6, song7, song8, song9, song10]
Thanks!
Friedemann Wachsmuth
1,231 PointsI got stuck with that one yesterday too. After realizing that += is no longer supported to append to Arrays (in Swift 1.1 that is), I almost gave up frustrated...
It worked then like this though:
var song1 = ["title": "Take on me", "artist": "a-ha", "album": "Hunting high and low"]
var song2 = ["title": "Thursgutter", "artist": "BSF", "album": "Der Chor steht auf"]
var songs = [song1]
songs.append(song2)
I am now able to access particular songs like this:
println(songs[0])
println(songs[1])
For some unknown reason, this does NOT work though:
println(songs.1)
I don't understand why. I also failed on directly accessing a particular dict's value via array index and key.
Jake Webb
13,028 PointsThanks for the Insight! I didn't know that += is no longer supported to append to Arrays. You also gave me some ideas on how to name my dictionaries.
c a
591 PointsOn cloud 9 since finally getting this one down. Here is what I did:
var As = ["title":"As","artist":"Stevie Wonder","album":"Songs in the key of life"] var runners = [ "title": "Runners", "artist": "Lecrae", "album": "Anomaly"] var billiejean = ["title": "Billie Jean" , "artist": "Michael Jackson", "album": "Thriller"] var nuthin = ["title": "Nuthin", "artist": "Lecrae", "album": "Anomaly"] var babybemine = ["title": "Baby be Mine", "artist": "Michale Jackson", "album": "Thriller"] var Power = ["title": "Power", "artist": "Kanye West", "album": "My Dark Twisted Fantasy"] var wewillrockyou = ["title": "We will rock you", "artist": "Queen", "album": "A night at the opera"] var Exodus = ["title": "Exodus", "artist": "Bob Marley", "album": "Exodus"] var Nothing = ["title": "Nothing", "artist": "Whitney Houston", "album": "Whitney"] var America = ["title": "America", "artist": "Prince", "album": "Around the world in a day"]
var song = (As,runners,billiejean,nuthin,babybemine,Power,wewillrockyou,Exodus,Nothing,America)
// pulling up a song:
song.1 song.4
// correcting Michale Jackson to Michael Jackson:
babybemine.updateValue("Michael Jackson", forKey: "artist")
//but this doesn't seem to go through to the Array, So I used this and it works better:
song.4.updateValue("Michael Jackson", forKey: "artist")
// check to make sure it changed:
song.4
// Using Print Commands:
print(song.4)
// selective print
print(song.4["title"])
// returns "Optional("Baby be Mine")"
// Hope this helps. I had to think about this one. Great lectures. Also thanks for all the help noted above. The initial answers were a great starting point.
Lukes Ivi
2,001 PointsFriedemann,
If I'm not mistaken,
println(songs.1)
Doesn't work because dot notation accesses the variables properties. "1" isn't a property. That's why it has to be accessed through [].
That's my guess.
Kass Yassin
1,589 PointsHmm confused about the above comment ^^ I managed to use dot notation println(songs.3) ... did I do something wrong?
var Hurt = ["title": "Hurt", "artist": "NIN", "album": "Downward Spiral"]
var DopeShow = ["title": "Dope Show", "artist": "Marily Manson", "album": "Mechanical Animals"]
var Fetisha = ["title": "Fetisha", "artist": "Orgy", "album": "Candy Ass"]
var SpecialK = ["title": "Special K", "artist": "Placebo", "album": "Black Market Music"]
var SelfEsteem = ["title": "Self Esteem", "artist":"Offspring", "album":"Smash"]
var songs = (Hurt, DopeShow, Fetisha, SpecialK, SelfEsteem)
//this printed: "Optional("Special K")"
println(songs.3["title"])
sam chacko ninan
2,278 Pointshere is my code var song1 = ["KY": "Key", "AR": "Artist", "AL": "Album"] var song2 = ["KY2": "Key2", "AR2": "Artist2", "AL2": "Album2"]
var song = [song1,song2]
how can i access the value "key2" from the array song
Jason Hernandez Berland
1,575 PointsJason Hernandez Berland
1,575 Pointsnot sure why I can;t mention you Jacob. Must be a Treehouse bug. Were you able to confirm that you met the extra credit requirements?