What is the array data type in iOS?

Arrays

Arrays are one of the most commonly used data types in iOS development, as they can be used to organize your app’s data. In addition, they can take any kind of elements from strings, integers, floats, doubles, classes, etc. Arrays are created with the square brackets and can be declared as empty. Furthermore, they are only be mutable if assigned to a variable. A constant array cannot be mutated. For example:

import Swift
let primeNumbers: [Int] = [2, 3, 5, 7]
let cites: [String] = ["agos", "New York", "Istanbul"]
var temperature: [Double] = [23.54, 26.44, 54.39, 46.88]

Arrays can be accessed using their indices, which start from zero and go up to the array’s count less than one. Elements in an array are ordered and have specific position at specific indices.

import Swift
cities[0]
// this accesses the first element in the cities array. "Lagos" precisely.

Arrays can also infer value type using Swift type inference when a specific type is not declared with the array. They can also hold multiple types, but it must be declared as an array of “Any” for it to compile. Below are examples.

import Swift
let numbers = [1, 2, 3, 4, 5]
// this will be inferred as an array that will only hold the integer type.
let names = ["Joan", "Jeff", "Lupita"]
// this will be inferred as an array that will hold only string type.
let multiple: [Any] = [3, "Nigeria", 66.78]
//this array can any type of data

Array properties are used to access an array’s element using dot notation. It is used by calling the array name followed by the property to be used. Examples include:

  1. .isEmpty: This checks and returns true if an array is empty.
  2. .count: This checks the number of elements in an array.
  3. .first: This returns the first element in an array.
  4. .last: This returns the last element in an array.

These are just a few properties, and they can be used in the following manner:

import Swift
primeNumbers.count
//This returns 4 since the array contains four elements
cities.isEmpty
//This returns false since cities array does contain elements

Array methods

Array methods are operations that can be performed on arrays. They also use dot notation in their usage. Examples include:

  1. .append(): This adds an element to the end of an array.
  2. .insert(_at:): This inserts an element into a specific position described using the arguments of the method.
  3. .remove(_at:): This deletes an element from a specific index described in the arguments of the method.
  4. .removeLast(): This removes the last element in an array. Arrays can also be transformed using the map(), flatMap(), compactMap(), and reduce() methods.

Arrays can also be added together using the + operation, but the arrays have to hold values of the same type.

import Swift
var citiesInAfrica: [String] = ["Lagos", "Timbuktu", "Nairobi"]
citiesInAfrica.append("Accra")
// citiesInAfrica now becomes ["Lagos", "Timbuktu", "Nairobi", "Accra"]
var citiesInEurope: [String] = ["London", "Berlin", "Paris", "Madrid", "Athens"]
citiesInEurope.removeLast()
// citiesInEurope now becomes ["London", "Berlin", "Paris", "Madrid"]
var allCities = citiesInAfrica + citiesInEurope
// allCities variable will now be ["Lagos", "Timbuktu", "Nairobi", "Johannesburg", "Accra","London", "Berlin", "Paris", "Madrid"]

Iterating over an array

Iterating over an array is checking through an array element by element to perform a method or search for an element.

import Swift
var citiesInAfrica: [String] = ["Lagos", "Timbuktu", "Nairobi"]
for city in citiesInAfrica {
print("\(city) is located in Africa.")
}
/* This prints
Lagos is located in Africa
Timbuktu is located in Africa
Nairobi is located in Africa
*/

The above is an example of iteration through an array to perform a certain operation.

More complex operations can be done using iteration. These can be found using Apple’s Swift Documentation.

Free Resources