When working with arrays, it’s possible that we’ll need to add more elements after they’ve been initialised. By calling the array’s append method, we can add a new element to the end of the array in Swift:
As an example, let’s say we have a
var shapes = ["Circle", "Rectangle"];
We can add a new item
"Triangle" to the end of the array as follows:
shapes.append("Triangle"); // [Circle", "Rectangle", "Triangle"]
Alternatively, we can use the addition assignment operator
+= to append an array of one or more items:
shapes += ["Triangle", "Square"]; // [Circle", "Rectangle", "Triangle", "Square"]
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.