Immutable/Mutable Collections in Swift

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

Leave a Comment