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 Basics (retired) Collections Modifying an Array

How to add multiple items to an array

Instead of having to add the separately: todo.append ("1") todo.append("2")

2 Answers

Stone Preston
Stone Preston
42,016 Points

you can append an array to the end of another array. so you would just put your multiple items you wanted to add to the array between [], and then add that to your other array.

Using +=

var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
//append multiple items to the end of cityArray
cityArray += ["Vancouver", "Los Angeles", "Eugene"]

Using .append

var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
//append multiple items to the end of cityArray
cityArray.append(["Vancouver", "Los Angeles", "Eugene"])

Thank you!!

cityArray.append(["Vancouver", "Los Angeles", "Eugene"])

Does it! My mistake was ommiting the parentheses