Accessing Elements
Part of the Fundamentals section of Coddy's Swift journey — lesson 63 of 86.
Now that you know how to create arrays, let's learn how to access individual elements within them. Each element in an array has a position called an index, and Swift uses zero-based indexing—meaning the first element is at index 0, not 1.
To access an element, use square brackets with the index number:
let fruits = ["Apple", "Banana", "Orange", "Mango"]
print(fruits[0]) // Apple
print(fruits[2]) // OrangeYou can also access the first and last elements using built-in properties:
let numbers = [10, 20, 30, 40]
print(numbers.first!) // 10
print(numbers.last!) // 40The count property tells you how many elements are in the array, which is useful when you need to access the last element by index:
let colors = ["Red", "Green", "Blue"]
print(colors.count) // 3
print(colors[colors.count - 1]) // BlueBe careful—accessing an index that doesn't exist will crash your program. If an array has 3 elements, valid indices are 0, 1, and 2.
Challenge
EasyYou are provided with the following array:
let cities = ["Tokyo", "Paris", "London", "Sydney", "Cairo"]Using array indexing and properties, print the following information on separate lines:
- The first city (using index)
- The third city (using index)
- The last city (using the
countproperty to calculate the index) - The total number of cities in the array
Your output should be:
Tokyo
London
Cairo
5Cheat sheet
Arrays use zero-based indexing, meaning the first element is at index 0. Access elements using square brackets with the index number:
let fruits = ["Apple", "Banana", "Orange", "Mango"]
print(fruits[0]) // Apple
print(fruits[2]) // OrangeAccess the first and last elements using built-in properties:
let numbers = [10, 20, 30, 40]
print(numbers.first!) // 10
print(numbers.last!) // 40Use the count property to get the number of elements in an array:
let colors = ["Red", "Green", "Blue"]
print(colors.count) // 3
print(colors[colors.count - 1]) // Blue (last element by index)Note: Accessing an index that doesn't exist will crash your program.
Try it yourself
let cities = ["Tokyo", "Paris", "London", "Sydney", "Cairo"]
// TODO: Write your code below
// 1. Print the first city (using index)
// 2. Print the third city (using index)
// 3. Print the last city (using the count property to calculate the index)
// 4. Print the total number of cities in the arrayThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input12Arrays Basics
Declaring An ArrayAccessing ElementsModifying ArraysArray MethodsRecap - Product ListRecap - Reversed ArrayTuples