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 Price

Carson Carbery
Carson Carbery
9,876 Points

Build a Vending Machine - Updating the price - Where is the amountDeposited being changed?

Hi I am following this lesson in my own xcode and it all works. However when we update the price the amountDeposited is somewhere having the item.price subtracted from it. I can't find where this happens, though I have made many searches and read all the code. Can some one explain how this happened to me please. Thanks

2 Answers

Carson Carbery
Carson Carbery
9,876 Points

I fixed this by adding a totalPrice variable, which i updated in the didSelectItemAtIndexPath and then subtracted from amountDeposited in the Purchase function. There's more to be done in the next lesson to clean this up for multiple lessons I guess, and I'm assuming that there is a code mismatch between the version being shown in the demo and the code that was being entered in the lesson. Either that or I missed something. Anyway onwards :-)

Zack Kitzmiller
Zack Kitzmiller
2,182 Points

This happens in VendingMachine.swift on (my) lines 129-131

        let totalPrice = item.price * quantity
        if amountDeposited >= totalPrice {
            amountDeposited -= totalPrice
        } else {