Container types are data types that are useful for containing other types. We'll spend this section covering the most prominent features of the most widely known and used containers:
- Array
- Dictionary
- Set
Arrays are unordered collections of the same element; dictionaries are keyed collections of elements; sets are unordered collections of unique elements.
Unlike in Objective-C, a Swift array can only hold elements of the same type, and it's a value type.
The following code declares an Array
container of three Int
types, 1
, 2
, and 3
:
let ints = [1,2,3] // Array<Int> or [Int]
As an example, if you wanted them to be Doubles
, you could easily force the type on it:
let doubles: [Double] = [1,2,3]