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
Marcos Reboucas12,890 Points
Isn'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?
Jhoan Arango14,575 Points
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.
If I am not answering the question right, please try to explain a bit further what you are asking.