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
Nasrullah Thajudeen
890 Pointstuple & dictioneries
... whats wrong with the below code [ i just wanted to give the song number and then be able to get the song details [ album,artist, title]
let song1 = ["Title": "Afraid","Artist": "Eminem", "Album": "Mockingbird"] let song2 = ["Title": "Move Ur Self","Artist": "Eminem", "Album": "Mockingbird"] let song3 = ["Title": "One love","Artist": "Eminem", "Album": "Mockingbird"] func trackDetail (#trackNumber: Dictionary <String,String>)->(Title:String,Artist: String,Album: String){
if trackNumber == song1 {
var musciAlbum = song1["Album"]
var musicTitle = song1["Title"]
var musicArtist = song1["Artist"]
if trackNumber == song2 {
var musciAlbum = song2["Album"]
var musicTitle = song2["Title"]
var musicArtist = song2["Artist"]
}
if trackNumber == song3 {
var musciAlbum = song3["Album"]
var musicTitle = song3["Title"]
var musicArtist = song3["Artist"]
}
}
let music = (musicAlbum,musicArtist,musicTitle)
return music
}
trackDetail(trackNumber:"song1")
2 Answers
Maximiliane Quel
Courses Plus Student 55,489 PointsHi,
I am not sure this is the most elegant version but you could do this:
- first you need to add a type for the key and value when you declare the dictionary as the type of the argument
- the return you want is of type string
- then you want to create variables that access the individual elements of your dictionary and store them so you can use them when you construct the string that you want to return. you can either concatenate or use string interpolation. you need to unwrap the variables. you do not need a conditional statement because you are sending in the respective song as a parameter
- when you call the function you should not put βsong1β into quotes since it is a constant not a String
the code would look like this:
let song1 = ["Title": "Afraid","Artist": "Eminem", "Album": "Mockingbird"]
let song2 = ["Title": "Move Ur Self","Artist": "Eminem", "Album": "Mockingbird"]
let song3 = ["Title": "One love","Artist": "Eminem", "Album": "Mockingbird"]
func trackDetail (#trackNumber: Dictionary<String, String> )-> String {
var album = trackNumber["Album"]!
var artist = trackNumber["Artist"]!
var title = trackNumber["Title"]!
return "Album: \(album) , Artist: \(artist), Title: \(title))"
}
trackDetail(trackNumber: song1)
Does that help?
Maximiliane Quel
Courses Plus Student 55,489 PointsYou are welcome :0)
Nasrullah Thajudeen
890 PointsNasrullah Thajudeen
890 Pointsthis is what i am looking for , thanks a lot for your time, much appreciated