Swift: Arrays

Arrays are a very important and are used very often as data type in Swift. Arrays can be seen as a collection of values that are store into one single constant or variable.

Creating an array is very simple in Swift, we simply separate all the values by commas and put them between square brackets. All the values that are stored must be of the same type, we cannot mix for example numbers and text!

let numbers : [Int] = [1, 2, 3, 4, 5]

Note that when we declare the variable type of an array, we also need to put it between square brackets.

We can also create an array that is initially empty, but we need to specify the type of data that is included.

let text : [String] = “”

The different values which are stored in an array have an index and this is the way that we access them. Note however, that the first value in an array has an index of 0! (not 1 as we may be tempted to think).

So, in order to output the first value, we will need to proceed as follows:

var animals : [String] = [“cat”, “dog”, “fish”]

print(animals[0]) // output: cat

Let’s now take a look at some properties that we can use with Arrays

Insert a new value in the array

To add a new value, we can use the “append” property which will add the value of the end of the array. In order to add multiple values with append, we will need to use “append(contentsOf:)

var animals : [String] = [“cat”, “dog”, “fish”]

animals.append(“horse”) //to add a single value

print(animals) // output: [“cat”, “dog”, “fish”, “horse”]

animals.append(contentsOf: [“horse”, “mouse”]) // output: [“cat”, “dog”, “fish”, “horse”, “horse”, “mouse”]

Or we can use the “insert ( at:)” property in which we will specify in which position to add the new value (“at:”). Again, to insert multiple values we use the contents of so “insert(contentsOf: at:)

var animals : [String] = [“cat”, “dog”, “fish”]

animals.insert(“mouse”, at: 0)

print(animals) // output:[“mouse”, “cat”, “dog”, “fish”]


Remove a value from an array

To remove a value from an array, we simply use “remove(at:)” or if it is the last element from the array, we can also use “removelast()”

var animals : [String] = [“cat”, “dog”, “fish”]

animals.remove(at: 0)

print(animals) // output:[“dog”, “fish”]


print(animals) // output: [“fish”]

Finally, if we want to empty quickly the entire array, we use the function “removeall()”.

Find the number of values in array

To find the number of values in array we need to use the “.count”

var animals : [String] = [“cat”, “dog”, “fish”]

print(animals.count) // Output: 3

One thought on “Swift: Arrays

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: