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
Rani Shayto
769 PointsCreating a function that returns either a title or an artist from a defined Dictionary in the output form of True/False
Questions are in the code as comments:
//: Playground - noun: a place where people can play
import UIKit
func nowPlaying (#songInfo: String) -> (Bool) {
let song1 = ["title":"Snow", "artist":"Rani"]
let x = song1["title"]
let y = song1["artist"]
var found = false
for x in song1 {
if x == songInfo {
found = true
}
else if y == songInfo {
found = true
} else {
found = false
}
}
return found
}
nowPlaying(songInfo: "Rani")
//I want to create the function nowPlaying for which I can input either the title or the artist of my song1 dictionary. If what I input into the function is in fact found in my dictionary, I want a boolian return of True, indicating I do have the information (either the title or the artist) in my song1 dictionary. Otherwise, I want the function to return a false, indicating that I do not have such information (either title or artist) in my song1 dictionary
//Error: line 15: Binary operator '==' cannot be applied to operands of type "(String,String)" and 'String
2 Answers
James Attree
7,906 PointsThe error is quite descriptive, it's saying that you can't compare a (String, String) to a (String). When you type:
let x = song1["title"]
x is actually (title, Snow)
When you try to compare this to the String "Rani" it will give you that error because the types are different. You need to find a way to loop through the dictionary. You can do this with a for:
for (title, artist) in song1 {
// Inside here, you have access to both the title and the artist as a string
}
func nowPlaying (#songInfo: String) -> (Bool) {
let song1 = ["title":"Snow", "artist":"Rani"]
for (title, artist) in song1 {
if (title == songInfo) {
return true
} else if (artist == songInfo) {
return true
}
}
return false
}
Rani Shayto
769 PointsThank you James. Sometimes I forget the basics I recently learnt, and when reading what you had suggested It took me a while to remember if I passed this information in the basics videos or not. Im not sure I did. However, it works! I have a question though, why aren't title and artist written down in this form ("Title, Artist") as a 1 String within quotations?
Thank you
James Attree
7,906 PointsI believe you're talking about this snippet:
for (title, artist) in song1
'title' and 'artist' are local variables that are created for the scope of the for loop. I could have called them anything at all, they just refer to the 0th and 1st items in the Dictionary ("title" and "song").
Rani Shayto
769 PointsRani Shayto
769 PointsEdit: since the line numbers don't show above, the error I got is for the line "if x == songInfo {