Junior — Middle
To which categories do collections in Swift belong?
sobes.tech AI
Answer from AI
In Swift language, the main categories of collections are:
- Arrays (Array) — ordered collections of elements of the same type, indexed by numbers.
- Sets (Set) — unordered collections of unique elements of the same type.
- Dictionaries (Dictionary) — collections of key-value pairs, where keys are unique.
Each of these collections has both mutable and immutable versions, depending on whether they are declared with var or let. For example, an array declared with let will be immutable.
Example:
var numbers: [Int] = [1, 2, 3]
var uniqueNumbers: Set<Int> = [1, 2, 2, 3]
var dict: [String: Int] = ["one": 1, "two": 2]