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
Sean Krause
2,155 PointsUsing a key from a class call dictionary value in a control flow statement.
Hello all!
Let me start by saying i am a beginner, i have been coding for about 2 months now and am completely new to swift.
Sorry about the wordy title but i couldnt figure out any other way to word it haha, maybe you can help me on that too!
I have been trying to do this for a couple days and cant seem to find an answer. I have exhausted all the tools in my toolbox on this one. I need to take the title from the book call in the catalogue dictionary and add it into a control flow statement so if someone checks out a book, it will show checkedOutBooks dictionary that it is either checked out or available. I have attempted to do this in a for loop but i cant figure out how to pull out just the title from the catalogue dictionary.
I get this error, not sure why.
--> Expression pattern of type 'String' cannot match values of type 'Book' <--
All the code also wont go in the code snippet for some reason.
Please let me know if i could help you help me.
class Book {
var title: String!
var author: String!
init(_ bookTitle:String, _ bookAuthor:String) {
title = bookTitle
author = bookAuthor
}
}
class Library
{
var catalogue = ["ORW":Book("1985", "George Orwell"),
"RAY":Book("Fahrenheit 451", "Ray Bradbury")]
var checkedOutBooks = [String:Person]()
func searchByTitle(_ title:String) -> String {
// TODO: This function searches the catalogue dictionary for a title
//
// Returns "Available" if the book exists and isn't checked out
//
// Returns "Checked out by name" if the book exists and is checked out
//
// Returns "Not in catalogue" if the book doesn't exist
for (_, value) in catalogue {
switch value {
case "1985": return"Available"
case "Fahrenheit 451": return"Available"
default: return("Not in catalogue")
}
}