Swift provides 3 inbuilt collection types; Arrays, Sets and Dictionaries.

Arrays

Arrays are ordered collections, where each information is added in an ordered list. Each element in the arrays is zero-indexed.

Array Initialization:

To initialize/create an empty array or an array with the predetermined values,

var intArray = [Int]()  // Creates an empty integer array
var stringArray = [String]()  // Creates an empty string array
var stringArray2 = ["dog", "cat", "mouse"]  // Creates an array with a set of elements
print( intArray )  //=> []
print( stringArray )  //=> []
print(stringArray2 )  //=> ["dog", "cat", "mouse"]

Arrays in Swift are type-safe, which means an array containing one type of elements can’t have any other type of element.

// Create a string array with integer and float type elements.
var typeSafeArray = ["dog", "cat", "mouse", 5, 10.12] // Throws an Error
Count the items:

The “count” property is very handful to count the total number of items contained in the array.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
print(pets.count)  //=> 5
Empty array check:

The “isEmpty” property is used to check whether the array contains any items or not!

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
print(pets.isEmpty)  //=> "false"
var cars = [String]()
print(cars.isEmpty)  //=> "true"
Add elements to the array:

To add an item at the end of the array,

var pets = ["dog", "cat", "mouse"]
// Using append method
pets.append("rabbit")
print(pets)  //=> ["dog", "cat", "mouse", "rabbit"]
// Using adding assignment operator
pets += ["hamsters"]
print(pets)  //=> ["dog", "cat", "mouse", "rabbit", "hamsters"]
pets += ["horse", "goat", "cow"]
print(pets)  //=> ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "goat", "cow"]

To insert an element at the specified index of the array, the “insert” method can be useful.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
pets.insert("horse", at:3) // Insert an item at the 4th position.
print(pets)
Access array elements:

To retrieve an item from the array.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "goat"]
var pet1 = pets[0]  // the first item starts with '0' index.
var pet2 = pets[4]  
print(pet1)  //=> "dog"
print(pet2)  //=> "hamsters"
Modify array elements:

The index value of the element is required to modify the element. For example,

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "goat"]
pets[6] = "fish"
print(pets)  //=> ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "fish"]
Remove an item:

Following example illustrates how to remove an element at a specific index.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
let removed = pets.remove(at:2) // remove 3rd item
print(pets) //=> ["dog", "cat", "rabbit", "hamsters"]
print(removed)  //=> "mouse"

To remove the last item of the array, the “removeLast” method is very handy.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
pets.removeLast()
print(pets)  //=> ["dog", "cat", "mouse", "rabbit"]
Slicing arrays:

An array in swift can be sliced using ellipsis (…) symbol specifiying the range of item indexes. Insted of modifying the main array, swift creates a new sliced array from the given range.

// the main array
var pets = ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "cow"]
// a new sliced arrays
var range1 = pets[0...2]  // first 3 items
var range2 = pets[4...6]  // last 3 items 
print(range1)  //=> ["dog", "cat", "mouse"]
print(range2)  //=> ["hamsters", "horse", "cow"]
// the main array is "untouched"
print(pets)  //=> ["dog", "cat", "mouse", "rabbit", "hamsters", "horse", "cow"]
Iterate over arrays:

Iterating over arrays in swift is very simple using the For-in loop.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
for item in pets {
print(item) 
}
//=> dog
//=> cat
//=> mouse
//=> rabbit
//=> hamsters

If the index of the item is also required, then it is best to use “enumerated” method. The enumerated method returns a tuple consist of indexes and values.

var pets = ["dog", "cat", "mouse", "rabbit", "hamsters"]
for (index, item) in pets.enumerated() {
print(index, item) // comma separated strings automatically be printed with spaces
}
//=> 0 dog
//=> 1 cat
//=> 2 mouse
//=> 3 rabbit
//=> 4 hamsters

Summary

In this tutorial, we focused on arrays in the Swift programming language. We covered how to create an array, how to perform basic CRUD operations, various array properties and how to use arrays in the for-in loop. We will be discussing “Set” collection type next!