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

iOS Build a Vending Machine App in Swift Displaying Additional Views Comparing Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Small Glitch in the video's code. Here's how to fix.

When the "Alert" is dismissed and the display is reset, the balance is also reset to zero, but you don't lose your money.

The offending line is in the dismiss alert function in ViewController.swift ~ line 139.

func dismissAlert(sender: UIAlertAction) -> Void {
    updateDisplayWith(balance: 0.0, totalPrice: 0.0, itemPrice: 0.0, itemQuantity: 1)
}

There are two ways to fix this: Just remove the balance parameter from the updateDisplayWith function call

updateDisplayWith(totalPrice: 0.0, itemPrice: 0.0, itemQuantity: 1)

Or... Leave the balance parameter in, but use vendingMachine.amountDeposited for the value passed in.

updateDisplayWith(balance: vendingMachine.amountDeposited, totalPrice: 0.0, itemPrice: 0.0, itemQuantity: 1)

Just an FYI. :dizzy:

Tagging: Pasan Premaratne

Ben Moore
Ben Moore
22,588 Points

Was about to make the same comment. Good catch.

Kareem Jeiroudi
Kareem Jeiroudi
14,984 Points

Correct! Thanks for commenting on this in the questions section 👍!

2 Answers

Actually it should be:

func resetVals(action: UIAlertAction){
        updateDisplayWith(balance: vendingMachine.amountDeposited, totalPrice: 0, price: 0, itemQuantity: 1)
        currentSelection = nil
    }

because after you make an invalid purchase and you hit purchase again, the old selection is used in giving the old error instead of the invalid selection error.

Anthia Tillbury
Anthia Tillbury
3,388 Points

Watching this I too thought it was odd that one's balance would need to be wiped.

Special thanks doesitmatter.