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

Has anyone posted correct answer for the Swift Dictionary extra credit w/ the songs array?

Not sure which answer is correct. Can someone look at my code? Implemented this 2 different ways. So i took a stab at it 1st time and came up with this:

var title=[ "S1": "Protect Ya Neck",
            "S2": "It Ain't Hard to Tell",
            "S3": "Flava In Ya Ear",
            "S4": "Dead Presidents",
            "S5": "Born In The USA",
            "S6": "The Most beautifullest Thing In The World",
            "S7": "Superman Lova",
            "S8": "N***** In Paris",
            "S9": "Peter Piper",
            "S10": "Dream On" ]


var artist=[ "S1": "Wu Tang Clan",
    "S2": "Nas",
    "S3": "Craig Mack",
    "S4": "JAY Z",
    "S5": "Bruce Springsteen",
    "S6": "Keith Murray",
    "S7": "Redman",
    "S8": "The Throne",
    "S9": "Run DMC",
    "S10": "Aerosmith" ]


var album=[ "S1": "Enter the Wu-Tang (36 Chambers)",
    "S2": "Illmatic",
    "S3": "Project: Funk da World",
    "S4": "Reasonable Doubt",
    "S5": "Born in the U.S.A.",
    "S6": "The Most Beautifullest Thing in This World ",
    "S7": "Whut? Thee Album",
    "S8": "Watch The Throne",
    "S9": "Raising Hell",
    "S10": "Aerosmith"]

Then I saw @jacobwebb code and changed mine.

var song1 = ["title": "Protect Ya Neck", "artist": "Wu Tang Clan", "album": "Enter the Wu-Tang (36 Chambers"]
var song2 = ["title": "It Ain't Hard To Tell", "artist": "Nas", "album": "Illmatic"]
var song3 = ["title": "Flava In Ya Ear", "artist": "Craig Mack", "album": "Project: "Funk Da World"]
var song4 = ["title": "Dead Presidents", "artist": "JAY Z", "album": "Reasonable Doubt"]
var song5 = ["title": "Born In The USA", "artist": "Bruce Springsteen", "album": "Born In The USA"]
var song6 = ["title": "The Most beautifullest Thing In The World", "artist": "Keith Murray", "album": "The Most Beautifulest Thing In The World"]
var song7 = ["title": "Superman Lova", "artist": "Redman", "album": "Whut? Thee Album"]
var song8 = ["title": "N****s In Paris", "artist": "The Throne", "album": "Watch The Throne"]
var song9 = ["title": "Peter Piper", "artist": "Run DMC", "album": "Raising Hell"]
var song10 = ["title": "Dream On", "artist": "Aerosmith", "album": "Aerosmith"]

2 Answers

Hi jaytova _,

A while back I posted my solution in another thread which for me is the cleanest way to construct the dictionary of songs.

https://teamtreehouse.com/forum/swift-extra-credit-challenge-dictionary

Hey Chris Upjohn good looking out! One question:

Can you explain this line?

[Dictionary<String, String>]

From what I gathered, this line is not necessary b/c Swift infers that this is a dictionary and a string? Or am I misunderstanding? Why would a programmer make this statement?

I included an explicit data type to help the compiler, if I didn't include this the compiler would have needed to parse the entire array first and then assign the type whereas this way tells the compiler exactly what values are accepted allowing for much faster inline parsing when you're still writing your code.

Personally I prefer to be explicit as it allows other developers to understand what the code is doing without having to guess everything, as explained in the course data types are optional but I think it's good practice to have them specified on all variables and constants.

That is indeed inferred, so you don't really need to use it. I've found it incredibly useful to do that, because when I go back a day later, I can very easily browse over my code and get back into the swing of things!

Thanks Chris Upjohn and Dylan Bailey, that makes plenty of sense