Link Menu Expand Document

Swift Programming

Arrays Series · Lesson 2

Arrays: Programming Tutorial

Follow a practical coding walkthrough for creating, reading, and updating arrays in Swift.

Swift Programming Xcode iOS

What to focus on while watching

  • Practice creating arrays with clear type declarations.
  • Use indexing and common methods to read and update elements.
  • Understand how array operations connect to real app logic.

Lesson Notes and Walkthrough

This lesson moves from concept to code. You will practice the most common array operations: creating, reading, appending, and removing values.

Core Operations

  • Create: start with a list of values.
  • Read: access an item using an index.
  • Update: change a value at a specific index.
  • Append/Remove: add or delete items as data changes.

Step-by-Step Example

var tasks = ["Study Swift", "Build App"]

tasks.append("Review Notes")   // add item
tasks[0] = "Study Arrays"      // update first item
tasks.remove(at: 1)            // remove second item

print(tasks)

Expected Output

["Study Arrays", "Review Notes"]

How to Debug Array Code

  1. Print the array after each major step.
  2. Check array.count before using indexes.
  3. Use clear names like tasks, scores, cities.

You may also like

Practice: Array Operations

~8 min Beginner

Goal: Add, remove, and read items from a Swift array.

Steps

  1. Create an array with two fruits.
  2. Append one item and remove one item.
  3. Print the final array and item count.

Expected Output

["apple", "orange"]
Count: 2
Need a hint? Use append to add and remove(at:) to remove.

Code

import Foundation

var fruits = ["apple", "banana"]
fruits.append("orange")
fruits.remove(at: 1)

print(fruits)
print("Count: \(fruits.count)")