We learned one way to create an array, but there’s another.
We can also declare and initialize an array with a list of values, separated by commas, and surrounded by a pair of square brackets.
The syntax to create an array this way looks like:
var name = [list of values]
Suppose we want to create an array called
location with Codecademy HQ’s latitude and longitude:
var location = [40.7245, -73.9979]
Due to Swift’s built-in type inference, the type of the array is inferred. To be explicit:
var location: [Double] = [40.7245, -73.9979]
Let’s try creating arrays using this new approach.
Suppose the Tokyo Subway costs are as follows:
Create an array named
subwayAdult and initialize it with
Print it out using
750. This time, be explicit with the type.
Then print it out.