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 trialMarcos Reboucas
12,890 PointsIsn't "return vendingMachine.selection.count" in collectionView's numberOfItemsInSection method "hard coded"?
When returning "return vendingMachine.selection.count" in collectionView's numberOfItemsInSection method, we're simply using the number of items in our 'selection' array implementation:
let selection: [VendingSelection] = [.Soda, .DietSoda, .Chips, .Cookie, .Sandwich, .Wrap, .CandyBar, .PopTart, .Water, .FruitJuice, .SportsDrink, .Gum]
But what if any of these items didn't exist in our data source, i.e. if we had got the data from the web? Shouldn't it be more appropriate to create an array with our 'inventory' objects and use it's count instead?
1 Answer
Jhoan Arango
14,575 PointsHello:
I am not 100% I understand your question..
But the count() method takes care of that.. if the array changes, the count will return the appropriate numbers of items in it. SO if you add or delete, you will always get an accurate count. So it's not "hardcoded" per say.
Hardcoded would be if I was to set the number of items with an integer.
return 2
If I am not answering the question right, please try to explain a bit further what you are asking.
Thank you
Marcos Reboucas
12,890 PointsMaybe I didn't explain myself correctly. What I meant is the array 'selection' is hardcoded inside VendingMachine Class. If you go to the plist, delete all the data and run the app you still have all 12 items in your screen, although of course they don't have any quantity or price anymore, and the app won't be able to throw any error in this situation. I know the implementation works in this example because we have the data inside the app and already know what will be 'received' but in a real scenario we should show to the user only the items we do have in our data source.
Chris Stromberg
Courses Plus Student 13,389 PointsChris Stromberg
Courses Plus Student 13,389 PointsSounds like you are answering your own question!?
If you already know what buttons your vending machine is going to have why can't you set it to the array of selections?