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

Help 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.

I'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...

func songInformation(myFavoriteSong = ["title": "Raju", "artist": "John McLaughlin", "album": "The Boston Record"]) -> (String, String, String) {

}

But I have no idea if that is accurate or where to go from that point.

I've been trying to figure this out also but no luck.

7 Answers

// 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)

}

Hey 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?

This 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

This 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"])

Haven'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.

When 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

Found 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?

Thanks Martin, that makes a lot more sense now. I didn't even think to try this with other data types.

I did: <p>func accept(#dictionary: Dictionary) -> (String, String, String) {

}</p>

I didn't create the actual dictionary but that's how you accept one.

I'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&lt;String, String&gt;. 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 &lt;String, String, String&gt; since there were 3 parameters in the dictionary.

func test (dict: [String: String]) -> (title: String, artist: String, album: String) {

return (title: dict["title"]!, artist: dict["artist"]!, album: dict["album"]!)

}

Don'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)