Swift Basics: Arrays

Collections and tuples
Types of collections:
Arrays, dictionaries, and sets.
All items stored in collections have to be of the same type.
Collections can be constant or variable. (Use let or var)

Arrays
Each item in an array is an element.
We can initialise arrays like this:
let firstArray = [1, 2, 3]
We can also declare them like this (these arrays are empty at first):
var secondArray = [Int]()
var thirdArray: [Int] = []
We can create arrays to store String, Double, SelfCreatedObjects
We can create arrays to store more than one type:
var fourthArray: [Any] = [1, "Three"]
We can also create arrays that can store objects of any class type:
var fifthArray:[AnyObject] = []
We can initialise an array to have 5 elements, each having the number 1:
var sixthArray = [Int](repeating: 1, count: 5)
We can create 2 dimensional arrays:
var twoD1 =[[1,2], [3,4],[5,6],[7,8]]
var twoD2 = [[Int]]()

To access the elements of an array:
It starts with 0:
print(firstArray[0])
//will print 1
To access 2d arrays:
let value = twoD1[0][1]
//Will print 2
To get the first item of the array:
let first = firstArray.first
let last = firstArray.last
let twoD1First = twoD1[0].first
let twoD1last = twoD1[0].last
let getAnArrayFrom2D1 = twoD1.first

Counting the elements of an array:
firstArray.count
twoD1[1].count

Check if an array is empty:

if firstArray.isEmpty{}

Appending a new value to an array:
firstArray.append(4)
OR
firstArray += [4,5]

Insert a value into an array:
firstArray.insert(7, at: 3)
//will insert the number 7 at the 4th spot

Replace elements in an array:
firstArray[0] = 9

Remove elements from an array:
firstArray.removeLast()
firstArray.remove(at: 2)
firstArray.removeAll()
var abc = firstArray.removeLast()
variable abc will get the last element in the firstArray.
var def = firstArray.remove(at: 2)
variable def will get the element at the 3rd spot.

Merging two arrays:
var combinedArray = firstArray + secondArray

Get elements in the 3rd to the 5th spot in the array:
var subArray = firstArray[2...4]
From the 3rd to the fourth spot:
var subArray = firstArray[2..<4]

One-sided range operators:
let firstArray = [1, 2, 3, 4, 5]
var x = firstArray[..<3] //contains 1, 2 and 3
var y = firstArray[...3] //contains 1, 2, 3 and 4
var z = firstArray[2...] //contains 3, 4, 5

To make multiple changes to an array:
var firstArray = [1, 2, 3, 4, 5]
firstArray[1...2] = [9, 10]
This will change the second and third element.
If it is:
firstArray[1...3] = [9, 10]
The fourth element will be removed.
You can also make changes where more elements are added/replaced:
firstArray[1...3] = [11, 12, 13, 14, 15]
the second to fourth element will be replaced with the elements with values from 11 to 15.

You can sort an array
var firstArray.sort(){$0 < $1}
The above will sort it in ascending order.
firstArray.sort(){$1 < $0}
The above will sort in descending order.

The sorted method produces a new array instead of changing the original:
let sorted = firstArray.sorted(){$0 < $1}

You can use a filter array to return a new array by filtering the original.
let filtered = firstArray.filter{$0 > 2 && $0 <10}
To filter string arrays:
I want to filter out the words that contain the letter o
let filtered = namesArray.filter{$0.range(of:"o") != nil}

range(of:) method returns true if the string contains the letter.

How to divide all the elements of an array by 5:
let mapAppliedArray = firstArray.map{$0/10}
Filter all the elements such that they all start with ID:
let applied = firstArray.map{"ID: \($0)"}
The applied array should contain: "ID: 1", "ID:2", etc...

Print every element in the array:
firstArray.forEach{print($0)}

Iterating over every element in an array:
for singleItem in firstArray
{
 print(singleItem)
}

To get the index and value for each item in an array:

var firstArray = ["John", "Henry", "Mary"]
for (index, value) in firstArray.enumeration()
{
 print("\(index) \(value)")
}

Comments

Popular posts from this blog

Setting up a playground

Go to another page