Posts Boring guide to Arrays in Swift
Post
Cancel

Boring guide to Arrays in Swift

  • Arrays are a ordered collection of items of similar type.

  • Same values can repeat in arrays as many items as we want.

  • Arrays use generics to define what type of collection of data will they store inside them

  • There are several ways to declare arrays but in each form you have to tell what type of data is being stored.

1
2
3
4
var arr1: Array<String> = Array<String>()
var arr2 = Array<String>()
var arr3: [String] = [String]()
var arr4: [String] = []

All of the above example decalre an empty array of Strings.

Initializing arrays

We can add items into an array either at the time of declaration or after declaring it. There are several initializers provided by the Array type.

  • This is the simplest way of declaring an Array. We just provide a commaseperated list of items to array

1
2
var arr: [String] = ["Item 1", "Item 2", "Item 3", "Item 4"]
print("arr: \(arr)")

Output:

1
arr: ["Item 1", "Item 2", "Item 3", "Item 4"]
  • We can pass the comma seperated list into one of the initializers as well.

1
2
var arr: Array<String> = Array<String>(["Item 1", "Item 2", "Item 3"])
print("arr: \(arr)")

Output:

1
arr: ["Item 1", "Item 2", "Item 3"]
  • We can create an array of repeated values very easily

1
2
var arr2 = Array<String>(repeating: "Item", count: 5)
print("arr2: \(arr2)"

Output:

1
arr2: ["Item", "Item", "Item", "Item", "Item"]
  • Infering Array type when declaring

    Swift can infer the array type if we declare an array with some initial values

1
2
var arr = ["Item 1", "Item 2", "Item 3", "Item 4"]
print("arr: \(arr)")

Output:

1
arr: ["Item 1", "Item 2", "Item 3", "Item 4"]

Mutability in Arrays

To create an array to/from which we can add/remove values whenever we want we should create an mutable array using var . If we create an immutable array using let while trying to modifying the content it will throw an error as shown in the image below.

1
2
3
4
5
6
7
8
var mutableList: Array<String> = ["John", "Marry"]
mutableList.append("Kellie")
print(mutableList)

let immutableList: Array<String> = ["John", "Marry"]
immutableList.append("Kellie")
print(immutableList)

Adding Arrays

We can always create a new arrays by adding 2 old arrays

1
2
3
4
var arr1: Array<String> = ["John", "Marry"]
let arr2: Array<String> = ["Kellie", "Mike"]
let arr = arr1 + arr2
print(arr)

Output:

1
2
["John", "Marry", "Kellie", "Mike"]

Adding elements into an Array

We can add elements using .append() method or .insert(, at:) method

1
2
3
4
5
6
7
8
var arr = ["Item 1", "Item 2", "Item 3", "Item 4"]
print("arr: \(arr)")

arr.append("Item 4")
print("arr: \(arr)")

arr.insert("Itemm", at: 3)
print("arr: \(arr)")

Output:

1
2
3
arr: ["Item 1", "Item 2", "Item 3", "Item 4"]
arr: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 4"]
arr: ["Item 1", "Item 2", "Item 3", "Itemm", "Item 4", "Item 4"]

Some commen Array Methods

  • .contains(Item) : This method returns a Bool true or false telling us whether the arguments passed into the method is present in the array or not.
1
2
print(arr.contains("Item 5"))
print(arr.contains("Item"))

Output:

1
2
true
false
  • .shuffle() : This method shuffles the array inplace and arranges the elements in a random order.
1
2
arr.shuffle()
print("arr: \(arr)")

Output:

1
arr: ["Item 1", "Item 4", "Item 2", "Item 6", "Item 4", "Itemm", "Item 3", "Item 5"]
  • .sort() : Like .shuffle() this one sorts the elements
1
2
arr.sort()
print("arr: \(arr)")

Output:

1
arr: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 4", "Item 5", "Item 6", "Itemm"]
  • .count() : Returns the number of elements present in the array
1
print(arr.count)

Output:

1
8
  • .removeFirst() : Removes the first element of the array
1
2
print(arr.removeFirst())
print("arr: \(arr)")

Output:

1
2
Item 1
arr: ["Item 2", "Item 3", "Item 4", "Item 4", "Item 5", "Item 6", "Itemm"]
  • .removeLast() : Removes the last element of the array
1
2
print(arr.removeLast())
print("arr: \(arr)")

Output:

1
2
Itemm
arr: ["Item 2", "Item 3", "Item 4", "Item 4", "Item 5", "Item 6"]
  • .remove(at:) : Removes the element at the particular index
1
2
print(arr.remove(at: 1))
print("arr: \(arr)")

Output: Item 3 arr: [“Item 2”, “Item 4”, “Item 4”, “Item 5”, “Item 6”]

1
  • .removeAll() : Remove all the elements of the array
1
2
print(arr.removeAll())
print("arr: \(arr)")

Output:

1
2
()
arr: []
  • .isEmpty() : Check if the array is empty or not and returns a boolean value.
1
print(arr.isEmpty)

Output:

1
true
This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags