O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing the array elements

The subscript syntax is used to retrieve values from an array. Subscript syntax, for an array, is where a number appears between two square brackets, and that number specifies the location (index) within the array of the element we wish to retrieve. The following example shows how to retrieve elements from an array using the subscript syntax:

let arrayOne = [1,2,3,4,5,6]  
print(arrayOne[0])  //Displays '1' 
print(arrayOne[3])  //Displays '4' 

In the preceding code, we begin by creating an array of integers that contains six numbers. We then print out the value at indexes 0 and 3.

One important fact to note is that indices in Swift arrays start with the number zero. This means that the first item in an array has an ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required