Section 1 - Instruction

Last time, we saw how to change existing items in an array using their index, like colors[1] = "yellow".

But what if you want to add a new item to the array without replacing an existing one.

Engagement Message

How might you add to the collection?

Section 2 - Instruction

Swift arrays have a special built-in method just for this: .append().

This method adds the specified item to the very end of the array. The array grows by one item.

Engagement Message

With me so far?

Section 3 - Instruction

Here's how you use it: arrayName.append(newItem).

If we have var tasks = ["review code"], and we run tasks.append("write tests"), the array changes.

Engagement Message

How long do you think the array will be after this operation?

Section 4 - Instruction

After running the operation the tasks array becomes ["review code", "write tests"]. The new item "write tests" is added at the end.

You can append items of any data type (strings, numbers, booleans) to an array, as long as they match the array's type.

Engagement Message

Do you think you can append an array to an array?

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal