Swift class function returning empty after for-in loop

802 Views Asked by At

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

There are 1 best solutions below

2
On BEST ANSWER

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
}