What is an Array?
An array is an ordered list with indices / Index.
Examples of an array are a to-do list, shopping list, Amazon Wish List, etc.
Let’s say we have an array list of apps
Index:
0 1 2 3
Element:
“Reminders” “Mail” “Xcode” “Calendar”
Important Note:
The index of the first element always starts with zero because the way computer scientists deal with data in memory, they have offset & offset starts with zero & that’s why the Array begins with zero. The index of an array never exceeds the number of elements.
Here is how to declare an Array in Swift:
We will declare an array with the let or var keyword, the identifier name, the equal sign, and square brackets. A comma separates each element.
var apps = ["Reminders", "Mail", "Xcode", "Calendar"] // Array of Strings, Each element is of Type String
var number = [1, 2, 300, 4000] // Array of Integers, Each element is of Type Integer
var bools = [true, false, false, true] // Array of Boolean values, Each element is of Type Bool
var groups = [["John", "Tom", "Steve"], ["Brandon", "Bill"]] // Array inside array
Here is how to copy one Array to a second array
var secondApps = apps // Key Values of apps array got assigned or copied to secondApp array
Here is how you can declare an Empty array for different types
Empty Array of Strings
var apps = [String]()
Empty Array of Integers
var numbers = [Int]()
Empty Array of Booleans
var bools = [Bool]()
Empty Array of Array Group of Strings
var groups = [[String]]()
Here is how you can access elements in Array using use subscript notation
var apps = ["Reminders", "Mail", "Xcode", "Calendar"]
let reminder = apps [0] // Index of First Element
let lastElement = apps[3] // Index of last element
Important Note:
A fast way to access any element inside the Array provides you with the exact index of your element. Remember that never let the index in subscript notation exceed the number of elements, as the program will crash if you do so.
Here is how we can know if the index does exceed or not.
The solution to this problem is to use one property called count.
var apps = ["Reminders", "Mail", "Xcode", "Calendar"]
let numberOfElements = apps.count
let firstElement = apps[0] // Accessing first element
let lastElement = apps[apps.count - 1] // Accessing Last Element of an array
let lastElement2 = apps[numberOfElements - 1] // Smart way of accessing Last Element of an array
Here is how you can find out if the Array is empty
apps.isEmpty // False
There are two types of Arrays, i.e. Mutable & Immutable Array
Mutable:
Elements can be changed after initialisation.
Immutable Array:
Elements cannot be changed.
Here is how to find which Array is mutable & which one is immutable.
The answer is simple:
If you declare an array using the let (constant) keyword, it is immutable; if you declare an array using the var (variable) keyword, it is mutable.
Things to do with a mutable array or modify the mutable Array
Here is how you can add a new element to the existing Array by Appending
apps.append("Safari") // Adding new element to array
Here is how you can remove an element at a specific index
apps.removeAtIndex(1)
Here is how you can remove the last element of an Array
apps.removeLast()
apps.insert(Keychain, atIndex: 3) // Inserting value at index 3 of an array
Here is how you can remove all elements in Array
apps.removeAll(keepCapacity: false)
Here is how you can replace an element at a specific index: 1
apps[1] = "Sketch"
I hope this blog post clarifies your doubts about Array in Swift Programming Language.
Thanks & Regards
Mandar Apte