Every programmer has knowledge about array like how to store, insert and retrieve data. but there is some type of confusion while writing code. so lets see some different kind of operation we can perform on array


Append always put item in last position.

var results: [Int] = []
//Output for result - [3,8,6]


Using insert we can place item at particular existing position.

var output:[Int] = []
output.insert(4, at: 0)
output.insert(6, at: 1)
//Output of output array - [4,6]

we can not insert item at position which is not exist. for ex. we can not insert item at index 1(second position) in empty array because index 1 is not exist.

Note: If we insert item at any index in array all the items in right hand side shifted one position to higher index.

For ex. if we 5 in output array again at index 0 then output array will become [5,4,6]  4 and 6 shifted to higher index.

Difference between Swift array and NSArray

Array is a struct, therefore it is a value type in Swift. NSArray is an immutable Objective C class, therefore it is a reference type in Swift.


var swiftArray = ["a", "b", "c"]
var objcArray: NSMutableArray = ["a", "b", "c"]

var swiftOtherArray = swiftArray
var objOtherArray = objcArray

swiftOtherArray[1] = "z"
objOtherArray[1] = "z"


Output for swiftArray -  ["a", "b", "c"]  //-- because its value type
Output for objcArray -  ["a", "z", "c"] //--- //-- because its reference type


Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions.  You can comment, like and follow posts.

You can request any topic related to Swift and iOS development.

Donate any small amount you think for this knowledge to grow this forum.                        

To Donate –  paypal.me/SandeshSardar