Using subscripting
The most convenient way to access elements in an array is by using the subscript syntax. This syntax lets you access any value directly by using its index inside square brackets:
var firstPlayer = players[0] print("First player is (firstPlayer)")
// > First player is "Alice"
Because arrays are zero-indexed, you use index 0 to fetch the first object. You can use a bigger index to get the next elements in the array, but if you try to access an index that’s beyond the size of the array, you’ll get a runtime error.
var player = players[4]
// > fatal error: Index out of range
Why did you get this error? It’s because players contains only four strings. Index 4 represents the fifth element, but there is no fifth element in this array.
When you use subscripts, you don’t have to worry about optionals, since trying to access a non-existing index doesn’t return nil; it simply causes a runtime error.