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 2.0 Using the Vending Machine Updating the Quantity

David Lin
David Lin
35,864 Points

Vending Machine bug & fix: Stepper's value needs to be reset to 1 in reset()

There's a bug in the Vending Machine code as presented.

Steps:

  1. Select an item.
  2. Increase the quantity by pressing "+" on the stepper to some greater value
  3. Switch selection to another item
  4. Increase the quantity by pressing "+" on the stepper to some greater value

Expected: After switching, the quantity of the new selection increments by 1 each time you press "+"

Actual: The quantity jumps up by the last quantity before switching selections. (e.g. If the last quantity was 3, then after pressing "+", the new quantity jumps from 1 to 4.)

Root Cause: The stepper's value was not reset to 1 after the selection switch, but remains at the last value before the switch.

Fix: Reset the stepper's value to 1 in reset()

  1. Hook up the stepper as an IBOutlet variable in the ViewController:

    @IBOutlet weak var stepper: UIStepper!
    
  2. Set the stepper's value to 1 in reset():

    func reset() {
        quantity = 1
        stepper.value = 1
        updateTotalPriceLabel()
        updateQuantityLabel()
    }