Module2: Challenge 10

So I just started working on Challenge 10 and ran into a bunch of different error messages. I am getting the error messages expected expression, and expected pattern, and also Cannot convert value of type ‘[String : Person]’ to expected argument type ‘String’. Any advice?

class Person{
    var name:String!
    
    init(_ fullName:String){
        name = fullName
    }
}

class Book{
    var title:String!
    var author:String!
    
    init(_ bookTitle:String, _ bookAuthor:String){
        title = bookTitle
        author = bookAuthor
    }
}

class Library{
    var catalogue = ["ORW":Book("1984", "George Orwell"), "RAY":Book("Fahrenheit 451", "Ray Bradbury")]
    
    var checkedOutBooks = [String:Person]()
    //let searchResult = lib.searchByTitle("1984")
    func searchByTitle(_ title:String) -> String{
        var bookFound = false
        var bookMessage = ""
        for 0..<catalogue.count{ in index
            if(catalogue[index].title == title){
                bookFound = true
                break
            }
        }
        if(bookFound){
            if(checkedOutBooks == title){
                bookMessage = "Checked out by name"
            }
            else{
                bookMessage = "Available"
            }
        }
        else{
            bookMessage = "Not in Catalog!"
        }
        //Available
        //Checkout
        //Not in catalog
        return bookMessage
    }
    
    func checkOut(_ bookId:String, _ person:Person) -> String{
        var bookFound = false
        var bookMessage = ""
        for 0..<catalogue.count{ in index
            if(catalogue[index].title == title){
                bookFound = true
                break
            }
        }
        if(book)
        //Returns error if out already
        //Returns success and adds
        //Returns book doesn't exist
        return bookMessage
    }
    
    func checkIn(_ bookId:String) -> String{
        //Returns doesn't exist
        //Returns error if book is not already out
        //Returns successful check in.
        return ""
    }
}

let lib = Library()
let borrower1 = Person("Curious George")
let borrower2 = Person("Mark Twain")

let searchResult = lib.searchByTitle("1984")
print(searchResult)

let borrowResult = lib.checkOut("ORW", borrower1)
print(borrowResult)

let searchResult2 = lib.searchByTitle("1984")
print(searchResult2)

let borrowResult2 = lib.checkOut("ORW", borrower2)
print(borrowResult2)

let checkInResult = lib.checkIn("RAY")
print(checkInResult)

First, you neglected to put an opening parentheses and flesh out one of your conditionals:

if(book)

If book what?

Secondly, the way you are looping over your catalogue dictionary and getting its items is not correct. Your dictionary has a key type of String, yet you are trying to subscript into it with an Int as if it was an array. Iterating through a dictionary is much different than iterating through a array. I would suggest stepping back and really rethinking your approach.

Thirdly, do the same with the Cannot convert value of type '[String : Person]' to expected argument type 'String' error.


I don’t want this to come across harsh or overly critical, but you are posting a lot of questions about things that you should be able to figure out on your own. You need to be able to debug and correct your code without asking for help on every little error. When errors pop up, sit back and examine your code. Try to figure out what the error message means and how you would fix it.

For instance, in the case of the last error above, what does it mean? You are trying to take a [String : Person] and use it where a String is expected. What type is checkedOutBooks? What type is title? Why can you not compare one to the other? What is it you are trying to do in this line and why doesn’t what you have do that?

If book was supposed to be if the book is found to exist then return an error if it is already checked out or a success if it is available to be checked out.

I am kind of frustrated myself with the Swift language. The reason I run in to so many errors is I don’t know the language as well as I would like to know it. The lessons and quizzes are quite good and I can easily follow them, but Module2 challenges kind of scaled up the difficulty quite a bit that I kind feel like I am bashing my head against a wall. I appreciate the help you and the other mods provide me, I really do. You guys do quite a lot to help me understand the difficulty of the language and make things easier for me to understand. I feel I learn quite a lot from both of you. :slight_smile:

If this was a language such as C++ or Ruby on Rails where I have a bit more proficiency in the language itself, then I would probably be asking a lot less questions. In Swift I don’t have that skill quite yet and it kind of takes me a lot longer to learn a new language such as this, because it is not something I am used to yet.

I honestly don’t know if the next set of challenges in the later modules will be as difficult as these or easier. I am kind of tempted to just skip the challenges entirely and just focus on the lessons and quizzes which are the areas I do a lot better on. I just feel like I am struggling quite a lot just to understand them and each little bit of progress I make, I wind up hitting the next wall after that. I am just feeling rather burned out by them.