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 Swift 2.0 Collections and Control Flow Introduction to Collections Adding Items to Arrays

Why can't I just go up to the original Array string and add the new item in there? Why do I need .append or +=?

Just want a use case example for why I would need to use .append instead of just going up to the original array and adding what I need to.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The idea here is that you won't always know in advance what's in the array. Most programs expect a certain amount of user input. For example if you were making a ToDo list application, you have no way to know what the user using your app needs to do. You'll let them add their to-do items themselves. So this is an instance where you would want to do this programmatically.

Anthony c
Anthony c
20,907 Points

For the example of a todo list, the user would fill out a form with their todo. They would click enter/save/submit or whatever your button is. That button would trigger some code that (1) takes their input from the form and (2) calls the append method which pushes the form input into the array (the array that holds all of their todos).

This is what the others mean by doing it "programmatically" and having a situation where you don't know what the user will want in the array (and the user obviously can't go into your code and edit the array themselves, they need the UI to "do it" for them)