Article by: Manish Methani
Published on: October 12, 2021 at 10:04am
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")
var arrayName = [Type]() Example :- var ageArray = [Int]()
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]
/* 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]
To access an array in swift, you can use a subscript index. The index starts with 0 in swift.
var variableName = arrayName[index]
import UIKit var agesArray:[Int] = [12,23,34] print("Values at first index (agesArray[1])" )
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)")
After using insert property array count is 2 After using remove property array count is 1
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.
import UIKit var agesArray = [Int]() agesArray.append(20) agesArray += [40] agesArray.append(60) print("Value at first index (agesArray[0])")
Value at first index 20
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) }
Codzify Amazon Google
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)") }
Value at index = 0 is Codzify Value at index = 1 is Amazon Value at index = 2 is Google
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) }
Codzify Amazon Google Manish Jez Larry
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") }
Age array count is 4 Name array is empty
Your go-to place to discover the Software Products, latest Tech News and Coding. Ignite Ideas, Unleash Innovation!
Codzify opens the door to a wealth of programming videos, free courses, mock tests, and endless opportunities for learning. Join us today and let your coding journey begin!
Start for Free. Login Now!Explore Coding in Simplified Way with Codzify.com
Online Coding Courses
Simplified Angular 16 Course from Basic to Advanced
Master the Skill of C Porgamming in 30 days.
Flutter App Development Course in Hindi
The Complete 2023 Web Development Bootcamp by Manish Methani | Udemy Course
Complete Flutter App Development Course for Beginners (2023) | Udemy Course
Quick Links