All About Array In Swift

Ekramul Hoque
Good Morning Swift
Published in
4 min readJun 2, 2018

Swift Most Use Collection Type|Generic Struct Type

Introduction : In iOS Development we use Array Every day life .There is many way we can create array .I think its More important to know what is Array and why we can create it many way .In this article i will explain what is array and how we can create it and modify update and delete element from array in many way .

What is Array :

Array in a common type of collection in swift .Array can be any time like Int, String, bool, Class or struct or any kind of data similar data type .

Example : If We have need to store 5 name of our daily shopping we will use Array for store these :

Here is 5 Item name we can store like this way .All item shorted by their index .index is from zero to number of item minus one .

Array can any kind of element like Int, String, Class, Struct and also Any

Array is common Genetic Struct Type in Swift for Storing collection of same type data

Array Mutability:

Array can be mutable or immutable like with var and let.If its var we can change it if let we cant change it.

So how do we create array :

There is many way to create array like with default initialiser ,with empty element etc.But Swift is type safe language we must assign type when create array .Two way to give type to swift compiler one is Implicitly or Explicitly.We Will discover all the way .

Create Empty Array :

//Empty Array with Implicitly definedlet arrayOne = Array<Int>() // Empty Array with Implicitly definedlet arrayTwo:Array<Int> = Array() //empty array let arrayThree:[Double] = [] //short fromlet arrayFour = [Double]() // short from 

We See here ,Need to Specify type because Array struct are generic type :

struct Array<Element> : RandomAccessCollection, MutableCollection {     //confirm all the protocol method 
//initialiser
//methode //subscripts
}

So Thats why We Need to Specify to Swift Compiler what will be the Element .

Creating Array with initialiser:

//array with initialiser1.let arrayFive = Array([1,3,4,5,6])//with array literal
2.let arraySix = Array(arrayLiteral: 1,2,3,4,5)
//array with convienence initiliser
3.let arraySaven = Array(1...5)
//array with buildin method repeat element
4.let arrayEight = Array(repeatElement("Ekram", count: 2))
arrayEight.count // 2

There all the way we can create array with default initialiser:

Array Methods :

There in Array Struct we have lots of Method and property built in for Accessing ,inserting, updating and removing element with the array .

I am discussing about those

Accessing Elements of an Array:

Lets Create an array called Name :

let name:[String] = ["Ekram","Moks","Rangon","Saikot","Ratul"]

Insert: We can insert element on this array using built-In insert method:

name.insert("hasan", at: 5)

Add element: we can add element using append method

name.append("Akash")

If We need to add more than one element to array we can use content Of :

name.append(contentsOf: ["Asif","Luna"])

Lasly we can add element by loop

for index in 1..<5 {          name.append(“Ekram”)       }

Updating :

using Subscript:

name[10] = "unknown"

We can insert multiple Element using Subscript using Like this

//updatename[1..<3] = ["Kamril","Ujjol"]

Removing element :

Remove by index :

name.remove(at: 0) //now 0 index string is removed 

Now at the index on 0 item is removed

Remove First :

name.removeFirst()

We can remove first Item from the array using this method

Remove Last :

name.removeLast()

Now from the name array last item is removed

Remove n number of element from the first :

name.removeFirst(2)

Now name array first two element are removed

Remove last n number :

name.removeLast(2)

Now last two number of element are removed from the array

Remove at Index :

name.remove(at: 1)

Now in the name array 1 number index element is removed

Remove All :

name.removeAll()

Now we can remove all element but toke capacity

removeAll(keepingCapacity: true)

Accessing Count :

name.count

Accessing capacity :

name.capacity

Adding two Array :

var nameTwo:[String] = ["Mozo","Tanvir"]var nameThree = name + nameTwo

Iterating Over:

We can iterate the element from the array using for-in loop :

for i in nameThree {
print(i)
}

We can even access index number and value using enumerate() method on the array

for (index,element) in nameThree.enumerated() {

print("\(index) number value is \(element) ")

}

Reordering Array Elements:

Using Sort and shored we can sorted element

nameThree.sorted()
nameThree.sort()

We can Use Array in Many way even in functional way use map, reduce, filter We Will Explore In next Article .Thank you Don’t forget to give me clap

--

--