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 Swiftlet 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 Swiftcities[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 Swiftlet 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:
.isEmpty: This checks and returnstrueif an array is empty..count: This checks the number of elements in an array..first: This returns the first element in an array..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 SwiftprimeNumbers.count//This returns 4 since the array contains four elementscities.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:
.append(): This adds an element to the end of an array..insert(_at:): This inserts an element into a specific position described using the arguments of the method..remove(_at:): This deletes an element from a specific index described in the arguments of the method..removeLast(): This removes the last element in an array. Arrays can also be transformed using themap(),flatMap(),compactMap(), andreduce()methods.
Arrays can also be added together using the + operation, but the arrays have to hold values of the same type.
import Swiftvar 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 Swiftvar citiesInAfrica: [String] = ["Lagos", "Timbuktu", "Nairobi"]for city in citiesInAfrica {print("\(city) is located in Africa.")}/* This printsLagos is located in AfricaTimbuktu is located in AfricaNairobi 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.