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
Ryan Lail
4,386 PointsCan anyone explain the 'Tuple Extra Credit' question to me?
I don't understand how a function will work with dictionaries or what the question actually wants me to return...?
3 Answers
Pasan Premaratne
Treehouse TeacherHi Ryan Lail,
I'll start with some pseudo code and if you need more specifics we can work from there.
Basically the question is asking you to write a function that accepts a dictionary as input.
Let's say the dictionary was something like this:
let musicDict = ["title": "Uptown Funk!", "artist": "Bruno Mars", "album": "Uptown Special"]
The function can accept this dictionary as an input and returns a named tuple. The element names in the tuple should match the keys in the dictionary and the values for each element in the tuple are values for the relevant keys in the dictionary
Ryan Lail
4,386 PointsI'm a bit stuck on how you would go from the dictionary to returning a tuple. Could you please explain that a bit more?
Pasan Premaratne
Treehouse TeacherSure. So given the music dict, all you're going to do is access each value and store it in a constant. For the key "title", store the value in constant; for example's sake we'll call it titleValue.
Then as your return value, you'll create a named tuple and return it with the value. Like so
return (title: titleValue)
The example isnt complete because I showed you only one value. Just repeat the same process for all three values
Ryan Lail
4,386 PointsHi, I've come up with this but i'm not entirely sure why xcode made me unwrap each Dictionary value?
let musicDict = ["title": "Uptown Funk!", "artist": "Bruno Mars", "album": "Uptown Special"]
func Tuple (#Music: Dictionary<String,String>) -> (titleValue: String, artist: String, album: String) {
let title = Music["title"]!
let artist = Music["artist"]!
let album = Music["album"]!
return (title, artist, album)
}
Tuple(Music: musicDict)
Pasan Premaratne
Treehouse TeacherThere is no guarantee that a value can exist for a particular key so when you retrieve a value from a dictionary it is always an optional. I would advise (strongly) that you avoid using "!" everywhere. It's pretty unsafe because if you pass in a dictionary to that function and "title" isn't a key, it will crash. Instead use if let, or guard let (this is a Swift 2.0 feature and the courses are being updated to add that).
You're correctly returning a tuple so yay!
Some stye/best practice pointers:
- function names are always camelCase. Start with a lower case and each subsequent word is uppercase. "someFunction" for example. So in this case your function should be called "tuple".
- Function names should always be very descriptive. "namedTupleFromDictionary" makes it obvious to anyone who uses it what the function does.
- Parameter names follow the same pattern so music should be lower case. Similarly you could make them more descriptive.
Other than that looks
Ryan Lail
4,386 PointsThanks for all the help! Could you show me how you would use the if-let statement instead of "!". I tried but ran into many errors. Thanks again!
Pasan Premaratne
Treehouse TeacherAnytime. Think of if let as a compound statement - a combination of a traditional if statement and an optional unwrap. Optionals exist because a value can potentially be nil right? So when working with optionals we first need to check if a value is nil and then use it. Without if let you could do something like this:
// Given some optional value
let someNumber: Int? = 3
if someNumber != nil {
let number = someNumber as? Int!
}
This way we're saying only if the value is not nil, then unwrap it. If let combines this into one statement.
if let number = someNumber as Int! {
// work with the number
}
You can combine multiple if let statements as well.
if let number = someNumer as Int!,
let string = someString as String! {
}
Using this method, we can safely use the values from the dictionary because they are only unwrapped if they're not nil. So the resulting code would be:
if let title = Music["title"] as? String,
let artist = Music["artist"] as? String,
let album = Music["album"] as? String {
return (title, artist, album)
}
However, a common complaint with if let statements is that code is nested which makes it hard to read. In Swift 2.0, the guard statement was introduced.
Guard is like if let but where in if let, the code inside the braces is the successful case, in guard it's the opposite. Your same code written with a guard statement would look like this:
guard let title = Music["title"] as? String,
let artist = Music["artist"] as? String,
let album = Music["album"] as? String else {
return nil
}
return (title, artist, album)
To understand guard a bit more check out our workshop on changes to Swift in 2.0.
Ryan Lail
4,386 PointsThanks!
Ryan Lail
4,386 PointsRyan Lail
4,386 PointsI don't understand what it means by return a named tuple?
Here's the question:
Extra Credit 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.