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 trialSara Worth
5,225 PointsThere is a bug in the Vending Machine app. How to fix?
If you make a selection and click "Purchase", it works as it's supposed to. But if you click "Purchase" again, it vends another of the same item, even though nothing is selected. There must be a way to zero out the selection after you click "Purchase"?
1 Answer
miikis
44,957 PointsHi Sara,
Sure there is. All you have to do is set currentSelection to nil after you call vendingMachine.vend in your purchase() function :)
Sara Worth
5,225 PointsSara Worth
5,225 PointsI tried that... I changed "if let currentSelection" to "if var currentSelection" then set currentSelection to nil. The compiler complained that "Nil cannot be assigned to type 'VendingSelection"
miikis
44,957 Pointsmiikis
44,957 PointsNo, that part is correct; it's just checking if currentSelection is nil or not. Remember, currentSelection is defined as a variable, optional property — of type VendingSelection — on the ViewController itself. So what you want to do is, right below that if...let...else, you want to set self.currentSelection to nil. Does that make sense?
Sara Worth
5,225 PointsSara Worth
5,225 PointsAh ok yes that worked! thank you :)
Nathan Tallack
22,160 PointsNathan Tallack
22,160 PointsI noticed the stepper does not reset either, so I added quantityStepper.value = 1 to fix that.