As the title says, what's the difference between Array vs NSArray vs [AnyObject]?
Also, what is most recommended way of approaching this. What i mean recommended is, what's the easiest implementation. Thank you.
As the title says, what's the difference between Array vs NSArray vs [AnyObject]?
Also, what is most recommended way of approaching this. What i mean recommended is, what's the easiest implementation. Thank you.
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 and it is bridged to Array<AnyObject>
.
NSMutableArray
is the mutable subclass of NSArray
.
var arr : NSMutableArray = ["Pencil", "Eraser", "Notebook"]
var barr = ["Pencil", "Eraser", "Notebook"]
func foo (var a : Array<String>)
{
a[2] = "Pen"
}
func bar (a : NSMutableArray)
{
a[2] = "Pen"
}
foo(barr)
bar(arr)
println (arr)
println (barr)
Prints:
(
Pencil,
Eraser,
Pen
)
[Pencil, Eraser, Notebook]
Because foo
changes the local value of a
and bar
changes the reference.
It will also work if you do let arr
instead of var
as with other reference types.
Array
is a Swift construct, and generic struct, which means that it can be an array of any specific type (Int, String, AnyObject, etc.)
[T]
is syntactic sugar for Array<T>
AnyObject
is an object of any class, including Objective-C classes.
NSArray
is an Objective-C construct that can hold any Objective-C object and is transparently mapped to and from Array<AnyObject>
Using the Krzak answer, here is a practical example:
// Let´s create an Array as a struct showing alternative ways
var arrStruct = ["Pencil", "Eraser", "Notebook"]
// or var arrStruct: [String] = ["Pencil", "Eraser", "Notebook"]
// or var arrStruct: Array = ["Pencil", "Eraser", "Notebook"]
// or var arrStruct = Array(["Pencil", "Eraser", "Notebook"])
// All this alternative ways create an array as struct
// Now let´s create a function that modifies this array struct
func modifyArr(alternativeArr: [String])
// or func modify(alternativeArr: Array<String>)
{
alternativeArr[2] = "Pen" // compilation error
// This won´t work. In swift >= 3.0 all func parametes are a let variable,
// this means alternativeArr is defined as a let. What one has to do is
// create a local variable and copy the value.
var localAlternativeArr = alternativeArr
// or var localAlternativeArr: [String] = alternativeArr
// or var localAlternativeArr: Array = alternativeArr
// now we can change it.
localAlternativeArr[2] = "Pen"
print(localAlternativeArr) // ["Pencil", "Eraser", "Pen"]
print(alternativeArr) // ["Pencil", "Eraser", "Notebook"]
}
modifyArr(alternativeArr: arrStruct)
print(arrStruct) // ["Pencil", "Eraser", "Notebook"]
// Since the arrStruct is a struct every time we assign to another variable or
// pass it as a func argument a copy is made.
// Now let´s create as an NSMutableArray
var arrClass: NSMutableArray = ["Pencil", "Eraser", "Notebook"]
// or var arrStruct = NSMutableArray(array: ["Pencil", "Eraser", "Notebook"])
// All this create an NSMutableArray as a class
// Now let´s create a function that modifies this array struct
func modifyArr(alternativeArr: NSMutableArray)
{
alternativeArr[2] = "Pen"
print(alternativeArr)
// (
// Pencil,
// Eraser,
// Pen
// )
}
modifyArr(alternativeArr: arrClass)
print(arrClass)
// (
// Pencil,
// Eraser,
// Pen
// )
// Since the arrClass is a class everytime we assign to another variable or
// pass it as a func argument is passed by reference. Means that any change
// inside modifyArr is going to change the arrClass outside. The change
// is made in the same pointer.
Adding to @Krzak's excellent answer, this is why
print(NSArray().object(at: 1)) // Triggers an UnmanagedException
2018-11-09 11:38:08.798088-0600 AppName[38786:10497909] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'
and
print(Array<Int>()[1]) // Halts with "Thread 1: Fatal error: Index out of range"
This different handling of the error helped me understanding the difference..... e