Chapter 04: Let’s Learn Swift Programming Language – Array in Swift

What is Array?

 
Array is an ordered list with indices / Index
Example of an Array can be 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: Index of first element is always start with zero because way computer scientist deal with data in memory they have offset & offset starts with zero & thats why array starts with zero. Index of an array never exceed the number of elements.

 

Here is how to declare Array in Swift: We will declare array with let or var keyword then identifier name then equal sign & then square brackets. Each elements are separated with comma.

 

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 second array

var secondApps = apps // Key Values of apps array got assigned or copied to secondApp array

 
Here is how you can declare 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: Fast way to access any element inside array provide that you know exact index of your element. Remember that never let the index in subscript notation exceed the number of elements as program will crash if you do so.

 

Here is how we can know if index does exceed or not?

 
Solution to this problem is we can 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 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 be changed

 

Here is how to find which array in mutable & which one is immutable array?
Answer is Simple: If you use let (constant) keyword to declare array then it’s immutable array and if you use var (variable) keyword to declare array then it’s mutable array.

 
Things to do with mutable array or modify mutable array

 
Here is how you can add new element to existing array by Appending

apps.append("Safari") // Adding new element to array

 
Here is how you can remove an element at 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 element at specific index 1

apps[1] = "Sketch"

 
I hope this blog post will clear all your doubts about Array in Swift Programming Language.

 
Thanks & Regards
Mandar Apte

Leave a Reply

Your email address will not be published. Required fields are marked *