Arrays, dictionaries, and sets
Swift offers a comprehensive set of collection types, as one would expect. In common with many other languages, each of these collection types will only hold values of the same type. Thus, the type of an Array
of Int
values is distinct from the type of an Array
of Float
values, for example. If you're coming from Objective C, you may quickly come to appreciate the type safety and simplicity of Swift Array
objects over NSArray
.
There are no separate mutable and immutable collection types, as such, since all objects in Swift can be declared with either var
or let
.
Arrays
Arrays are zero-based, and look like this:
let myArr = [21, 22, 23]
They are equipped with a pretty standard set of methods, such as count
and accessor methods:
let count = myArr.count // 3 let secondElmt = myArr[1] // 22 let firstElmt = myArr.first // 21 let lastElmt = myArr.last // 23
Elements are set, logically enough, as follows:
myArr[1] = 100
They are a lot more convenient to work with than...