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 Quantity

Where has the code gone??

Hey guys,

at 1.50 of this video, we're shown to add code to the total price. For some reason I have code in XCODE that is missing from the code.

Specifically here:

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        updateCellBackgroundColor(indexPath, selected: true)

        currentSelection = vendingMachine.selection[indexPath.row]
    ///THIS CODE ->   
 if let currentSelection = currentSelection, let item = vendingMachine.itemForCurrentSelection(currentSelection) {
            totalLabel.text = "$\(item.price)"
        }
    /// -<
    }

Where has it gone in the video? And why don't we need it anymore?

1 Answer

Paul Brazell
Paul Brazell
14,371 Points

Pasan refactored that code so it is now in a function called updateTotalPriceLabel() so you don't have to copy and paste the entire code into the places you need it. Instead you just call the function.