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 trialDavid Lin
35,864 PointsVending 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:
- Select an item.
- Increase the quantity by pressing "+" on the stepper to some greater value
- Switch selection to another item
- 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()
-
Hook up the stepper as an IBOutlet variable in the ViewController:
@IBOutlet weak var stepper: UIStepper!
-
Set the stepper's value to 1 in reset():
func reset() { quantity = 1 stepper.value = 1 updateTotalPriceLabel() updateQuantityLabel() }