Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App with Swift Creating a Data Model Creating a Data Collection

lance samaria
lance samaria
3,642 Points

Creating 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
Justin Black
24,266 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
lance samaria
3,642 Points

Thank you for the explanation.