In Swift, an array stores values of the same type in an ordered list.
To create an empty array of a certain type, we can use the initializer syntax:
var name = [Type]()
Suppose we want to create an empty array called scores
of the type Int
:
var scores = [Int]()
Now that we’ve initialized the scores
array, we can add integers (and only integers) inside.
Note: Arrays can only store items of the same data type. The type of the array cannot be changed after it’s declared.
We’ll also figure out how to remove, change, and manipulate the array later on, but let’s start with creating one.
Instructions
The Tokyo Subway has different payment options for adults and children.
Create an empty array named subwayAdult
of the type Int
.
Then print it out using print()
.
Create an empty array named subwayChild
of the type Int
.
Then print it out.