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 Decomposing a Tuple

Rani Shayto
Rani Shayto
769 Points

Dictionary function extracredit

Hello,

I am attempting to do the dictionary function extra-credit, and the longer I try the more I forget the basics of function I've learnt. I have given it a couple of shots on xCode but I know I am creating a mess. Would appreciate any examples with explanations behind the logic.

Thank you

Rani Shayto
Rani Shayto
769 Points

Here is what I have written down in xCode:

func song (#nameOfSong: Dictionary<String,String>) -> (title: String, artist: String) {

var musicDictionary1 = ["title": "Jack", "artist": "Daniels"]

var musicDictionary2 = ["title": "Ron", "artist": "Weasly"]

var musicDictionary3 = ["title": "Ben", "artist": "Afleck"]

return musicDictionary1 // error message: '[String : String]' is not convertible to '(title: String, artist String)'

}

1 Answer

Bharath Chandrashekar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bharath Chandrashekar
iOS Development with Swift Techdegree Graduate 12,055 Points

func createPlaylist(#playlist:[String:String]) -> (title: String, artist: String, album: String) {

//Creating a tuple from the dictionary given as input to this function
let playlistTuple = (playlist["title"]!,playlist["artist"]!, playlist["album"]!)

return playlistTuple

}

let returnedPlaylistTuple = createPlaylist(playlist: ["title" : "Chak de","artist": "Sukwinder","album": "Patriotic"])

let titleName = returnedPlaylistTuple.title //Prints "Chak de" let artistName = returnedPlaylistTuple.artist //Prints "Sukwinder" let albumName = returnedPlaylistTuple.album //Prints "Patriotic"