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 triallance samaria
3,642 PointsCreating a Data Collection (3:44) "Every time we press the button we are creating a new array" Please explain.
At 3:44 Pasan says “Now the other problem is that our code is insufficient. Every time we press the button, we’re creating a new array”.
Can someone please explain this? How are we creating a new array? I would think that every time the button is pressed the designated indexed item would be called from the same exact array. Why does the array change?
2 Answers
Justin Black
24,793 Points@IBAction func showFunFact() {
let factsArray = [“fact 1”, “fact 2”]
funFact.label.text = factsArray[1]
}
Being that @IBAction is an outlet containing a click event for our button we can conclude that any function within it is run every time the button is clicked.
So what he means is, clicking the button creates the array for that one time the user clicked the button. Clicking it again re-generates it. The factsArray array, is not stored as a parameter for the class, but as a local variable for the function.
lance samaria
3,642 PointsThank you for the explanation.