What is the difference between Array and NSArray?


Since Array and NSArray are both core constructs in iOS development, we sometimes forget just how different they are. Here are some major differences between the two constructs that you should know about.

  • 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.

  • Array is a Swift construct, and generic struct, which means that it can be an array of any specific type (Int, String, etc.). [T] is syntactic sugar for Array<T>. NSArray is an Objective-C construct that can hold any Objective-C object and is transparently mapped to and from Array<AnyObject>

  • An array can store one type of data, whereas NSArray can hold different types of data.

  • There are three types of arrays in Swift − Array, NSArray and NSMutableArray. While Objective-C has only two types of array.

Array Syntax

// declare an immutable array
let arrayName: [valuetype] = [array element 1, array element 1, ...array element n]
// declare a mutable array
var arrayName: [valuetype] = [array element 1, array element 1, ...array element n]

In Swift, we can declare the array with let for immutable and var for a mutable array. For an immutable array, we have to provide the array elements at the time of initialization.

Array

Example

Let's see an example to understand the difference −

var languagesArray = ["Swift", "Objective-C", "Java", "Python"]
func modifyArray1(array: [String]) {
   var inputArray = array
   inputArray[2] = "C++"
}
modifyArray1(array: languagesArray)
print(languagesArray)

Output

["Swift", "Objective-C", "Java", "Python"]

Explanation

In the above example, you can see the original array has not been changed.

NSMutableArray

Syntax

var arrayName: NSMutableArray = [element1, element2,..... elementN]

Example

We use NSMutableArray to declare a mutable array along with the elements. We can declare an empty array also to append the elements later in the code.

import Foundation
var languagesMutableArray: NSMutableArray = ["Swift", "Objective-C", "Java", "Python"]
func modifyArray2(array: NSMutableArray) {
   array[2] = "C++"
}
modifyArray2(array: languagesMutableArray)
print(languagesMutableArray)

Output

(
   Swift,
   "Objective-C",
   "C++",
   Python
)

Explanation

In the above example, we have declared a mutable array with some values. We make a function to append a new value in the array. You can see in the output that the original array has been changed.

Difference between Array and NSArray
Array NSArray/NSMutableArray
It belongs to Swift library. It belongs to the Objective-C foundation.
Provide more flexibility. A bit complex to use for beginners.
Use let and var keywords to declare an array. NSArray is immutable and NSMutableArray is a mutable type.
It is a struct type in Swift language. These are the class type of Objective-C.

Conclusion

Swift provides us with both types of arrays that are mutable and immutable. We can use them as per the requirement. If we don’t want to change the original elements of an array, we can make it constant using the let keyword otherwise var keyword will be used for a mutable array.

Updated on: 21-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements