Now that we have an array, how do we retrieve an individual value? This is where index comes into play.
An index refers to an item’s position within an ordered list. Arrays in Swift are zero-indexed, meaning the first item has index 0, the second index 1, and so on.
var vowels = ["a", "e", "i", "o", "u"]
- The item at index 0 is
- The item at index 1 is
- The item at index 2 is
- The item at index 3 is
- The item at index 4 is
To retrieve individual elements, we can use the subscript syntax,
array[index]. The subscript syntax uses square brackets,
], around an index value.
print(vowels) // Prints: a print(vowels) // Prints: e print(vowels) // Prints: i print(vowels) // Prints: o print(vowels) // Prints: u
The 24-hour weather forecast is recorded in an array called
temperature, starting with the current temperature as its first value.
Print the current temperature using its index.