0

I am trying to build a simple Library-Shelf-Book model in Swift and I am running into a strange issue. Within the following Library Class, my func total book count is returning 0 every time. I know its probably something simple I'm overlooking but any help would be awesome. Here is my library class:

class Library
{

var allShelves:[Shelf]=[]
var allBooksCount = 0

var description: String{
    return "This Library has \(allShelves.count) Shelves with \(allBooksCount) books"
}

func addNewShelf(newShelf: Shelf){
    var newShelf = Shelf()
    self.allShelves.append(newShelf)
    println("There are \(allShelves.count) SHELVES in this library")

}

func totalBookCount() -> Int{
    for currentShelf in allShelves{
        allBooksCount = currentShelf.numberOfBooks
    }
    return allBooksCount
}


}

Here is my Shelf class:

class Shelf
{

var allBooksOnShelf:[Book] = []
var numberOfBooks = 0

init(){
    self.allBooksOnShelf = []
}

var description: String{
    return "This Shelf has \(allBooksOnShelf.count) Books"
}

func addNewBook(newBookToAddToShelf: Book){
    let newBook = Book(bookName: newBookToAddToShelf.bookName)
    self.allBooksOnShelf += [newBookToAddToShelf]
    numberOfBooks = allBooksOnShelf.count
    println("new book called \(newBook.bookName)")

}
}

Here are my tests:

let newLibrary = Library()
//println(newLibrary.description)


let libraryShelf1 = Shelf()
newLibrary.addNewShelf(libraryShelf1)

let libraryShelf2 = Shelf()
newLibrary.addNewShelf(libraryShelf2)

libraryShelf1.addNewBook(Book(bookName: "Game Of Thrones"))
libraryShelf1.addNewBook(Book(bookName: "Hunger Games"))
libraryShelf1.addNewBook(Book(bookName: "Return of the Jedi"))

println("this Shelf has \(libraryShelf1.allBooksOnShelf.count) books")

newLibrary.totalBookCount()

println(newLibrary.description)

newLibrary.totalBookCount() always returns 0.

1
  • 1
    Use the debugger to trace through and watch the variable values. Commented Nov 13, 2014 at 17:23

1 Answer 1

1

I see 2 errors:

Error 1

func addNewShelf(newShelf: Shelf){
    var newShelf = Shelf()
    ^^^^^^^^^^^^
    self.allShelves.append(newShelf)
    println("There are \(allShelves.count) SHELVES in this library")        
}

Here you are redefining newShelf parameter as a local variable, losing what is passed in. The correct implementation is:

func addNewShelf(newShelf: Shelf){
    self.allShelves.append(newShelf)
    println("There are \(allShelves.count) SHELVES in this library")        
}

Error 2

Here:

func totalBookCount() -> Int{
    for currentShelf in allShelves{
        allBooksCount = currentShelf.numberOfBooks
    }
    return allBooksCount
}

at each iteration you reinitialize the allBooksCount property, so in the end what the func returns is the count for the last shelf in the loop. You should add instead (using the += operator) - moreover resetting the counter at beginning would also be a good idea (otherwise if you call the function multiple times you have incorrect results):

func totalBookCount() -> Int{
    self.allBooksCount = 0
    for currentShelf in allShelves{
        allBooksCount += currentShelf.numberOfBooks
    }
    return allBooksCount
}
Sign up to request clarification or add additional context in comments.

2 Comments

wow! thanks so much! i knew it was something small, this worked perfectly thanks!!
Glad it worked :) Don't forget to mark the answer as the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.