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

Sara Worth
Sara Worth
5,225 Points

There 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

Hi 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
Sara Worth
5,225 Points

I 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"

    @IBAction func purchase() {
        if var currentSelection = currentSelection {
            do {
                try vendingMachine.vend(selection: currentSelection, quantity: Int(quantityStepper.value))
                updateDisplayWith(balance: vendingMachine.amountDeposited, totalPrice: 0.0, itemPrice: 0, itemQuantity: 1)
                currentSelection = nil  // ERROR
            } catch VendingMachineError.outOfStock {
                showAlertWith(title: "Out of Stock", message: "This item is unavailable. Please make another selection")
            } catch VendingMachineError.insufficientFunds(let required) {
                let message = "You need $\(required) to complete the transaction"
                showAlertWith(title: "Insufficient Funds", message: message)
            } catch VendingMachineError.invalidSelection {
                showAlertWith(title: "Invalid Selection", message: "Please make another selection")
            } catch let error {
                fatalError("\(error)")
            }

            if let indexPath = collectionView.indexPathsForSelectedItems?.first {
                collectionView.deselectItem(at: indexPath, animated: true)
                updateCell(having: indexPath, selected: false)
            }

        } else {
            showAlertWith(title: "Please make a selection", message: "")
        }

    }

No, 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
Sara Worth
5,225 Points

Ah ok yes that worked! thank you :)

Nathan Tallack
Nathan Tallack
22,160 Points

I noticed the stepper does not reset either, so I added quantityStepper.value = 1 to fix that.