Swift Array

Article by: Manish Methani

Last Updated: October 12, 2021 at 10:04am IST
6 min 2 sec read

The array is used to store elements of the same type. Arrays in swift include strict checking which means Mutable means you can add, and modify the elements of an array whereas in the case of Immutable arrays you cannot.

Note:-

If an array is assigned to a variable then that array becomes Mutable and If an array is assigned to a "constant" then that array becomes Immutable. Create immutable array

let array = NSArray(array: ["First","Second","Third"])
          or
let array1 = ["First","Second","Third"]

Create mutable array

var array2 = ["First","Second","Third"]

Append object to array
array.append("Forth")

Syntax to create an empty array:-

var arrayName = [Type]()

Example :-
var ageArray = [Int]()

Syntax to create an array with repeating elements:-

var arrayname = [Type](repeating:InitialValue ,  count: NumbeOfElements)

/* Create an array with repeated Value say 23 */
var ageArray = [Int](repeating:23 , count:4 )

1) NumbeOfElements:- number of elements you want into an array.

2) InitialValue:- indicates the values of an array. Suppose you give it the value "10". Then all the elements of an array should have the value "10".

Create an array with different elements of same type:-

var arrayName : [type] = [1,2,3,4]
/* Create an array with Different values */
var ageArray:[Int] = [12,23,34]

Examples of different ways to create an array in Swift:-

 /* Create an empty array */ 
var ageArray = [Int]()

/* Create an array with repeated Value say 23 */
var ageArray = [Int](repeating:23 , count:4 )

/* Create an array with different values */
var ageArray:[Int] = [12,23,34]

How to access an array?

To access an array in swift, you can use a subscript index. The index starts with 0 in swift.

var variableName = arrayName[index]

Example:-

import UIKit

var agesArray:[Int] = [12,23,34]

print("Values at first index (agesArray[1])" )

Insert & remove the property of an array

insert property is used to insert an element at a specific index and remove property is used to remove an element at a specific index. but make sure the array should not go out of size.

import UIKit

var ageArray = [Int]( )

ageArray.insert(4, at: 0)
ageArray.insert(5, at: 1)

print("After using insert property array count is (ageArray.count)")

ageArray.remove(at: 0)
print("After using remove property array count is (ageArray.count)")

Output:-

After using insert property array count is 2
After using remove property array count is 1

How to modify an array?

You can use the append() method or addition assignment operator (+=) to add a new item at the end of an array. Be careful with syntax of declaring an array.

Example:-

import UIKit

var agesArray = [Int]()

agesArray.append(20)
agesArray += [40]
agesArray.append(60)

print("Value at first index (agesArray[0])")

Output:-

Value at first index 20

Iterating Over an Array

for-in loop is used to iterate over an array. Swift 3.0 also provides an enumerated() function to iterate over an array and in return, it returns an index of an item.

//: Playground - noun: a place where people can play

import UIKit

var stringArray = [String]()

stringArray.append("Codzify")
stringArray.append("Amazon")
stringArray += ["Google"]

for item in stringArray {
    print(item)
}

Output:-

Codzify
Amazon
Google

Enumerating an Array

Swift 3.0 also provides an enumerated() function to iterate over an array and in return, it returns an index of an item.

//: Playground - noun: a place where people can play

import UIKit

var stringArray = [String]()

stringArray.append("Codzify")
stringArray.append("Amazon")
stringArray += ["Google"]

for (index, item) in stringArray.enumerated() {
    print("Value at index = (index) is (item)")
}

Output:-

Value at index = 0 is Codzify
Value at index = 1 is Amazon
Value at index = 2 is Google

Creating an array by adding two arrays together

With the help of the + operator, you can add two arrays together which as a result forms a new array. But remember both arrays should be of the same type.

//: Playground - noun: a place where people can play

import UIKit

var string1Array = [String]()

string1Array.append("Codzify")
string1Array.append("Amazon")
string1Array += ["Google"]



var string2Array = [String]()

string2Array.append("Manish")
string2Array.append("Jez")
string2Array += ["Larry"]



var string3Array = string1Array + string2Array

for item in string3Array
{
  print(item)
}

Output:-

Codzify
Amazon
Google
Manish
Jez
Larry

Count Property & isEmpty Property of an array

count property is used to count the number of elements in an array. isEmpty property is used to check whether an array is empty or not.

//: Playground - noun: a place where people can play

import UIKit

var ageArray = [Int](repeating:23 , count:4 )
print("Age array count is (ageArray.count)")


var nameArray = [String]()
if(nameArray.isEmpty)
{
 print("Name array is empty")
}
else
{
 print("Name array is not Empty")
}

Output:-

Age array count is 4
Name array is empty

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com