Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. If you create an array, a set, or a dictionary, and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the collection after it’s created by adding, removing, or changing items in the collection. If you assign an array, a set, or a dictionary to a constant, that collection is immutable, and its size and contents cannot be changed.
An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.
let immutableArray: [String] = ["Alice", "Bob"] // Type of mutableArray inferred as [String] var mutableArray = ["Eve", "Frank"] // Test the membership let isEveThere = immutableArray.contains(“Eve") // Access by index let name: String = immutableArray // Update item in list; // crashes if the index is out of range mutableArray = "Bart" // immutableArray = "Bart" // Error: can't change mutableArray.append("Ellen") // Add an item // Add an item at index mutableArray.insert("Gemma", at: 1) // Delete by index let removedPerson = mutableArray.remove(at: 1) // You can't reassign a let collection nor change // its content; you can reassign a var collection // and change its content mutableArray = ["Ilary", "David"] mutableArray = “John"
A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.
// Sets ignore duplicate items, so immutableSet // has 2 items: "chocolate" and "vanilla" let immutableSet: Set = ["chocolate", "vanilla", '"chocolate"] var mutableSet: Set = ["butterscotch", "strawberry''] // A way to test membership immutableSet.contains("chocolate") // Add item mutableSet.insert("green tea") // Remove item, if the item isn't found returns nil let flavorWasRemoved: String? = mutableSet.remove("strawberry"
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to lookup values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.
let immutableDict: [String: String] = ["name": "Kirk", "rank": "captain"] // Type of mutableDict inferred as [String: String] var mutableDict = ["name": "Picard", "rank": "captain" // Access by key, if the key isn't found returns nil let name2: String? = immutableDict["name"] // Update value for key mutableDict["name"] = "Janeway" // Add new key and value mutableDict["ship"] = "Voyager" // Delete by key, if the key isn't found returns nil let rankWasRemoved: String? = mutableDict. removeValue(forKkey: "rank")