Immutable/Mutable Collections in Swift
There is only one Array
and one Dictionary
type in Swift. The mutability depends on how you construct it:
var mutableArray = [1,2,3]
let immutableArray = [1,2,3]
i.e. if you create an assign to a variable it is mutable, whereas if you create an assign to constant it is not.
WARNING: Immutable arrays are not entirely immutable! You can still change their contents, just not their overall length!
Arrays
Create immutable array
First way:
let array = NSArray(array: ["First","Second","Third"])
Second way:
let array = ["First","Second","Third"]
Create mutable array
var array = ["First","Second","Third"]
Append object to array
array.append("Forth")
Dictionaries
Create immutable dictionary
let dictionary = ["Item 1": "description", "Item 2": "description"]
Create mutable dictionary
var dictionary = ["Item 1": "description", "Item 2": "description"]
Append new pair to dictionary
dictionary["Item 3"] = "description"
More information on Apple Developer
Swift does not have any drop in replacement for NSArray
or the other collection classes in Objective-C.
There are array and dictionary classes, but it should be noted these are "value" types, compared to NSArray and NSDictionary which are "object" types.
The difference is subtle but can be very important to avoid edge case bugs.
In swift, you create an "immutable" array with:
let hello = ["a", "b", "c"]
And a "mutable" array with:
var hello = ["a", "b", "c"]
Mutable arrays can be modified just like NSMutableArray
:
var myArray = ["a", "b", "c"]
myArray.append("d") // ["a", "b", "c", "d"]
However you can't pass a mutable array to a function:
var myArray = ["a", "b", "c"]
func addToArray(myArray: [String]) {
myArray.append("d") // compile error
}
But the above code does work with an NSMutableArray:
var myArray = ["a", "b", "c"] as NSMutableArray
func addToArray(myArray: NSMutableArray) {
myArray.addObject("d")
}
addToArray(myArray)
myArray // ["a", "b", "c", "d"]
You can achieve NSMutableArray
's behaviour by using an inout
method parameter:
var myArray = ["a", "b", "c"]
func addToArray(inout myArray: [String]) {
myArray.append("d")
}
addToArray(&myArray)
myArray // ["a", "b", "c", "d"]
Re-wrote this answer 2015-08-10 to reflect the current Swift behaviour.