An array is a consecutive block of memory that holds a certain type of data. It can be a predefined data type, such as int
, float
, char
, string
, or bool
, or it can be user-defined. Also, arrays are zero-based, which means that the first object in the array is 0
.
Arrays can be either of the var
or let
type. If we create an array of the var
type, then we can change, add, or remove the objects from the array similar to how we have MutableArray
in Objective-C. If you want an ImmutableArray
array in Swift, instead of var
, use the let
keyword. Arrays in Swift can be declared and initialized as follows:
var score = [10, 8, 7, 9, 5, 2, 1, 0, 5, 6] var daysofweek = ["Monday", "Tuesday", "Wednesday"]
Now, if we want to declare an array and initialize it later, we can declare it as shown in the following snippet of code:
var score : [Int] var daysofweek : [String]
Here, you will have to provide the data type that the array will be storing. It is very similar to defining a regular variable; it...