O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appending to an array

A static array is somewhat useful, but having the ability to add elements dynamically is what makes arrays really useful. To add an item to the end of an array, we can use the append method. The following example shows how to append an item to the end of an array:

var arrayOne = [1,2] 
arrayOne.append(3)  //arrayOne will now contain 1, 2 and 3 

Swift also allows us to use the addition assignment operator (+=) to append an array to another array. The following example shows how to use the addition assignment operator to append an array to the end of another array:

var arrayOne = [1,2] 
arrayOne += [3,4]  //arrayOne will now contain 1, 2, 3 and 4 

The way you append an element to the end of an array is really up to you. Personally, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required