Link Menu Expand Document

Swift Programming

Conceptual Lesson 1

Arrays: Visual Explainer

Understand how arrays organize related values so you can model data cleanly before writing code.

Swift Programming Xcode iOS

What to focus on while watching

  • Arrays are ordered collections, so index position matters.
  • Each element should be the same data type for predictable behavior.
  • Use arrays when you need to loop, sort, filter, or update related values.

Lesson Notes and Walkthrough

An array is an ordered list of values. Think of it like a row of labeled lockers: each locker has a position number, and you can get or change values by position.

Key Ideas for Beginners

  • Arrays keep related data together in one place.
  • Order matters because each item has an index.
  • In Swift, indexes start at 0, not 1.

Basic Example

var colors = ["blue", "green", "red"]
print(colors[0]) // blue
print(colors[2]) // red

How to Read This

  1. colors is the array name.
  2. Each value has a position: 0, 1, 2.
  3. colors[0] means “get the first item.”

Common Beginner Mistakes

  1. Using an index that does not exist (for example colors[5]).
  2. Forgetting indexes start at 0.
  3. Mixing unrelated values in one array, which makes code harder to reason about.

You may also like

Practice: Array Thinking

~5 min Beginner

Goal: Create an array and inspect elements by index.

Steps

  1. Create an array of three favorite colors.
  2. Read the first and last value by index.
  3. Print a sentence using both values.

Expected Output

First: blue, Last: red
Need a hint? Index 0 is the first value. Use count - 1 for the last.

Code

import Foundation

var favoriteColors = ["blue", "green", "red"]
let firstColor = favoriteColors[0]
let lastColor = favoriteColors[favoriteColors.count - 1]

print("First: \(firstColor), Last: \(lastColor)")