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 Swift Functions and Optionals Parameters and Tuples Tuples

I am trying to do this extra credit question, could someone lead me in the right direction.

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.

func music (info:String) ->  track: String {

    var music = ["title":"Bad Girl", "artist":"usher", "album":"part2"]

    return music

}

music(info: "Bad Girl")

2 Answers

Sean Lee
Sean Lee
5,196 Points

You're trying to pass a String when you need to pass a Dictionary that contains the title, artist, album.

I've come up with the following:

let musicDictionary = 
   [
        "title":"Blank Space",
        "artist":"Taylor Swift",
        "album":"1984"
    ]

func musicTuple (#info: Dictionary <String, String>) -> (title: String, artist: String, album: String) 
{

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

musicTuple(info: musicDictionary).title
musicTuple(info: musicDictionary).artist
musicTuple(info: musicDictionary).album
Cory Braun
Cory Braun
1,204 Points

Thanks for posting your answer Sean. I'm still a little confused about a few things.

How come the Dictionary is declared first, and not declared inside the curly braces (as Amit does with the Array in the Tuple video)?

Also, in the return, why do we use exclamation points after "title"], "artist"] and "album"]?