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

Appending Arrays ...

I have been watching this section in Swift about appending arrays, which is something that all languages can do. in in swift you write out another line of code.

ArrayName.append(newElement)

so my question is this. if you are just creating the array to begin with, why the need to add an extra line of code to append it? why not just make the changes directly in the array itself ?

not sure the reasoning for appending something that has to change to begin with? I am also very new to programming so maybe i am missing the obvious ?

Hi James!

There will be a time when you may have an empty array to keep track of something. Like a student roster...

let studentRoster = ["harold", "james", "tiffany"]

then you may get a new student for the class... an you need the teacher to be able to add to the roster records(lame example sorry lol)

studentRoster.append("Gabby")

hopefully this helps buddy.

Harold,

thanks for the answer, I get the need to append, what i don't fully get is why go through the extra steps to add a new line of code to do this..

wouldent it be eaiser and faster to just drop a coma, and add a new name?

Oh! i see your concern its not so much the ability to add someone in code, rather in the future you will be the programmer and your program has users, of course the user does not see the code, so think of it like this the array is like i said a student roster, but as the programmer you may need to make some kind of function for when the student admin gets on the computer and adds a student. as the programmer it will look like this

let studentRoster = [""]

func addAStudent(name: String, Age: Int, Grade: Int) {
             studentRoster.append(name,Age, Grade) 
}


if (Administrator hits the  new student button) {
              addAStudent

}

for the Administrator it will just look like name:____ age:_____ grade:____ after they click on new student

let me know if this helps a little more