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
hitanshubhakta
4,350 PointsHelp with 'function that accepts a Dictionary as parameter'
Need help with Extra Credits of Parameters and Tuples in Swift Functions and Optionals. How to solve this? cannot figure out how to set Dictionary as Parameter of a function.
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.
Chris McKay
2,401 PointsI've been trying to figure this out also but no luck.
7 Answers
Garrit Jacobson
2,692 Points// This is what I came up with.
func treehouseExtraCredit(#musicDictionary: Dictionary<String, String>) -> (title: String, artist: String, album: String) {
let title: String = musicDictionary["title"]!
let artist: String = musicDictionary["artist"]!
let album: String = musicDictionary["album"]!
return (title, artist, album)
}
Martin Langeder
3,335 PointsHey I see why this solution is working, but I do not understand why you have to put the "!" at the end of the lines. Can you explain that?
Michael Waihura
6,963 PointsThis works for me too.I din't use the constants but instead made direct calls to the dictionary title,artist and album keys directly in the return statement. The "!", I think indicates optional when the value does not exist but I'll confirm after watching the video on optionals
Azin Mehrnoosh
13,754 PointsThis worked for me, too. Also, you have to pass the function something to this effect to get the Truple output:
treehouseExtraCredit(musicDictionary: ["title": "Strawberry Fields Forever", "artist": "Beatles", "album": "The White Album"])
William Cillekens
4,575 PointsHaven't gone through the Swift Functions and Optionals course yet, but I got this to work while playing around in the playground by specifying the Dictionary content types.
func songInformation(myFavoriteSong : Dictionary<String,String> = ["title": "Raju", "artist": "John McLaughlin", "album": "The Boston Record"]) {
}
Add the desired return types and appropriate return statement in the function and it should work.
Garrit Jacobson
2,692 PointsWhen I wrote the code Xcode threw up a bunch of warnings and the "fix it" option put in the unwrapping operator (!). I'm not 100% sure, but my best guess is the dictionary data type returns an optional in this case, and it needs to be unwrapped in order to assign it. I cant find any documentation on this though. Sorry I can't give you more than a guess. I'm very curious to know the real reason too, considering you can assign a dictionary value to a variable outside of a function without having to unwrap it unless you explicitly type it. Its kind of confusing. Try it in a playground, you will see what I mean
Martin Langeder
3,335 PointsFound this at developer.apple.com in the swift programming language resources: For a dictionary that stores String values, for example, the method returns a value of type String?, or “optional String”. So yes it returns a String?
Garrit Jacobson
2,692 PointsThanks Martin, that makes a lot more sense now. I didn't even think to try this with other data types.
Nicholas Lagerstedt
1,433 PointsI did: <p>func accept(#dictionary: Dictionary) -> (String, String, String) {
}</p>
I didn't create the actual dictionary but that's how you accept one.
Chris Newton
2,922 PointsI'm guessing here, so looking for someone to back up my logic or thought process. But with regard to the casting part of the function parameters, Dictionary<String, String>. When you set them both to be strings, are you casting the type of "key" and "value" to a String? Originally I thought there needed to be 3 in there, like <String, String, String> since there were 3 parameters in the dictionary.
Emre Sancaktaroglu
2,943 Pointsfunc test (dict: [String: String]) -> (title: String, artist: String, album: String) {
return (title: dict["title"]!, artist: dict["artist"]!, album: dict["album"]!)
}
Ben Abair
2,400 PointsDon't know if its correct but this is what worked for me.
let music: [String: String] = ["Song": "Bright Lights Bigger City", "Artist": "Ceelo Green", "Album": "The Lady Killer"]
func extraCredit(songList: [String: String]) -> [String: String]{ return music } extraCredit(music)
Zach Pratt
Courses Plus Student 12,756 PointsZach Pratt
Courses Plus Student 12,756 PointsI've been trying to figure this out as well. I can't find any documentation that gives the syntax for using a dictionary as a parameter of a function.
The only way I could get the information into Xcode without an error was...
But I have no idea if that is accurate or where to go from that point.