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 Displaying Additional Views Comparing Answers

Jacob Evans
Jacob Evans
2,979 Points

Quantity not completely resetting when selecting a different item?

First I select an item and change quantity then I change my mind and select a new item.

Counter resets on screen but when you click plus it just adds +1 to the original quantity of the previously selected item stepper count.

https://www.dropbox.com/s/in8jriq0h3pu5c8/vendingmachinequantity.mov?dl=0

3 Answers

Jesse DeOms
Jesse DeOms
18,815 Points

Hey Jacob, I was having the same issue!

My reset function sets my quantity variable to 1 when I switch items, but then when I try to increment or decrement the quantity using the buttons in the app, the quantity jumps to the stored sender.value (which is not being reset to 1 when I reset my quantity variable to 1)...

The best way that I have figured to fix this (after some serious googling) is to create a reference outlet for the button in our ViewControler file. We can use this reference outlet to assign a value of 1.0 to the counterValue as a part of our reset function ...

  1. Go to the assistant editor (the ven-diagram-ish double circle button in the top right of your screen)
  2. Open your Main.storyboard file on the left side of your screen and your ViewController file on the right side
  3. Control click on your Stepper button in the Main.storyboard file
  4. Select and drag Referencing Outlet to your View Controller file (give this UIStepper object the name "counterValue")
  5. In your reset function add the following line of code: counterValue.value = 1.0

I hope this helps!

Charlie O'Shea
Charlie O'Shea
18,737 Points

Yep this is quite an issue in the app and would have been a useful way to demonstrate IBOutlets for controls.

I think there's another problem with the quantity code but I'm not sure yet if it gets address later on in the course. It only emerges when testing out the machine. For example if you attempt to buy 2 sandwiches straight away it will give you an insufficient funds error, but it will also decrease the quantity of the sandwich stock by 2 without completing the purchase. If the user then tries to buy the 1 sandwich they've been informed that they can afford it will tell them Out of Stock.

I've fixed it by moving

item.quantity -= quantity
inventory.updateValue(item, forKey: selection)

to the bottom of the vend() function, or at least below the if block that tests if theres enough money deposited so the function throws the InsufficientFunds error before decrementing the quantity.

eberhapa
eberhapa
51,495 Points

I found the same issue and fixed it with the same solution. Its because InsufficientFunds is throwing the error below

item.quantity -= quantity

So its getting out of stock but you are actually not buying it because you are checking for funds later. By moving it to the end its called after all checks.

Jacob Evans
Jacob Evans
2,979 Points

I actually figured this out while at WWDC with the swift engineers.

The solution was pretty much what you said. I had to add this line to the top of ViewController: @IBOutlet weak var stepperQuantity: UIStepper!

And connect it to the stepper.

Then I reset that to 1.0 in the rest method: stepperQuantity.value = 1.0

However, was this ever mentioned in any of the videos? I could not find it or completely missed this step. I also tried looking in the project files and that stepper connection was not in any of the files I tried.

May need to update the video or add into the teacher's notes.

Glad you added the steps and answered the question because now it should show up in Google results.

Thanks