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

How do I tweak my code for the Extra Credit assignment about Parameters and Tuples to identify Key components of the dic

"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 successfully done so, here:

import UIKit

let favSong = ["Title": "Only The Good Die Young", "Artist": "Billy Joel", "Album" : "Rainy Day"]

func searchDic(input : [String: String]) -> (Title : String, Artist : String, Album: String) {

    return (input["Title"]!, input["Artist"]!, input["Album"]!)

}

let song = searchDic(favSong)
song.Title
song.Artist
song.Album

However, I'm unsure how useful this is and exactly what it is or isn't capable of accomplishing (meaning I guess I don't understand exactly what I did).

I would like to have it so when entering the function parameters I am asked for 2 of the 3 Keys, and have it give me the 3rd value.

Example:

func searchDic("Billy Joel", "Only the Good Die Young")

and the result should be it spitting out the "Album" name. In the same respect if I type in the Song Title and Album I'd like it to give me the Artist etc.

Is that what my program was initially meant to accomplish? Is such a task a little much for a beginner who's just recently finished the "Optionals" portion of the iOS lessons.

Thank you for your time and tutelage in advance :)

  • Rehan

I am definitely learning here, but thought I'd post what I did here, maybe it can help.

I got a ton of errors doing it this way and had to Google for some answers, but was able to get it working. It does seem to give me the specific information I'm looking for from the Tuple returned, but really not sure ... why or how. :)

// define some songs as Dictionaries
var YOLO = ["Title":"YOLO","Artist":"The Lonely Island","Album":"The Wack Album"]
var billieJean = ["Title":"Billie Jean","Artist":"Michael Jackson","Album":"Thriller"]

// function
func songList(#song: Dictionary<String, String>) -> (title: String, artist: String, album: String) {
    let title = song["Title"]
    let artist = song["Artist"]
    let album = song["Album"]

    return (title!, artist!, album!)
}

// assign to variables, one for each song
var song1 = songList(song: YOLO)
var song2 = songList(song: billieJean)

// pull certain return types from Tuple
song1.artist
song2.album