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

Adding multiple keys to a single Dictionary parameter? Not sure I understand the extra credit assignment

In Swift Functions and Optionals, under Parameters and Tuples, the extra credit assignment states:

Write a function that accepts a Dictionary as parameter and returns a named tuple. The dictionary should contain the following keys: title, artist and album.

How do I assign all three keys to a single Dictionary parameter? Or am I getting the gist of what the assignment is even asking?

func songs(??: Dictionary) -> (?,?) {}

that is one of many different variations I've tried, including trying to use <> because they showed up with autocomplete. Meanwhile I didn't find the developer library particularly helpful in finding an answer.

Please help!

2 Answers

Without outright giving you the direct answer, the Apple Docs actually have a pretty good guide that would help. Let me know if you need another clue/the answer. Sometimes it's better to just learn from the answer instead of banging your head on the keyboard!

Here's the link: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

Yes I've looked at that page before and it wasn't helping much. I understand the basics of creating a dictionary but am feeling unsure as to how they are used as parameters in a function. Which I don't see that anywhere on that Docs page. Basically, I'm still confused. Some things I've tried that don't seem to work.

func song(#title: String, #artist: String, #album: String) {}

func song(Dictionary) -> (#title: String, #artist: String, #album: String) {}

func song(#songs: Dictionary) -> (#title: String, #artist: String, #album: String) { var songsDic = [] }

... I don't know. I'm getting more lost the more I think about it. So yes I'd appreciate the answer. I'll learn much faster that way by picking apart the way it actually works.

I had the same problem, and also found the developer website uninformative.
I went back to the dictionary videos to see if i could unravel this, and realized that I didn't complete one of the extra credit problems, which is the first step in this extra credit!

Try going back to dictionaries and arrays, and solve that first. Even for that, I needed help!

I did a search, and found an answer on stack exchange. Reading very carefully, I realized that what we want is an array of dictionaries, and not a dictionary of dictionaries. adding multiple key value pairs ends up being straightforward. Just add a comma and add another pair!!

try this link if you're still having trouble! http://stackoverflow.com/questions/27045887/swift-add-multiple-key-value-objects-to-nsdictionary