Using a key from a class call dictionary value in a control flow statement.(Swift)

Updated on May 2, 2017 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on May 2, 2017

Hello all!

    • THIS IS SWIFT.

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’ <–

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”)
      }
}

  • Liked by
Reply
0 on May 2, 2017

Maybe you can help guide me to what i should have put into google to find an answer too? All help is greatly appreciated.

  • Liked by
Reply
Cancel