Arrays

Arrays are ordered lists — the workhorse collection of everyday code. By the end of this lesson you'll declare arrays of type [T] , add and remove elements, access by index safely, and loop over every value.

Learn Arrays in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Declaring Arrays

An array holds an ordered list of values of one type, written [Element] . Swift can infer the type from a literal, or you can annotate it — handy when starting empty.

2️⃣ Adding & Removing Elements

To change an array it must be a var . Use append to add to the end, insert(_:at:) at a position, and remove(at:) to delete.

3️⃣ Accessing Elements Safely

Arrays are zero-indexed: the first element is arr[0] . Reading an out-of-range index crashes , so prefer first / last (optionals) or check count when you're not sure.

4️⃣ Iterating Over an Array

A for-in loop visits each element in order. When you also need the index, enumerated() hands you (index, element) pairs.

Your turn. Fill in the blanks, then run it and check the output.

📋 Quick Reference

Build it, run it, and check your output against the example in the comments.

Practice quiz

How do you write the type of an array of Strings?

  • Array(String)

Swift array types are written as [Element], e.g. [String].

What does the first element of an array have as its index?

  • 1
  • 0
  • -1
  • It has no index

Answer: 0. Swift arrays are zero-indexed, so the first element is at index 0.

Which method adds an element to the end of a var array?

  • arr.add(x)
  • arr.push(x)
  • arr.append(x)
  • arr.insertLast(x)

Answer: arr.append(x). append(_:) adds an element to the end of the array.

How do you get the number of elements in an array?

  • arr.length
  • arr.size()
  • arr.count
  • arr.len

Answer: arr.count. The count property returns the number of elements.

What happens if you read arr[10] on an array with 3 elements?

  • Returns nil
  • Returns 0
  • Runtime crash (index out of range)
  • Returns the last element

Answer: Runtime crash (index out of range). Accessing an out-of-bounds index crashes at runtime.

Can you append to an array declared with ?

  • Yes, always
  • No — let arrays are immutable
  • Only if it's empty
  • Only the first time

Answer: No — let arrays are immutable. A let array is constant; use var to be able to append or modify it.

What does [1, 2, 3].isEmpty return?

  • true
  • false
  • 3
  • nil

Answer: false. isEmpty is false because the array has elements.

How do you create an empty array of Int that you can append to?

var nums: [Int] = [] makes a mutable empty Int array.

Which loop visits every element of an array ?

  • for item in items { ... }
  • loop items { ... }
  • each items { ... }
  • items.for { ... }

Answer: for item in items { ... }. A for-in loop iterates over each element directly.

Are all elements of a Swift array required to be the same type?

  • No, mix freely
  • Yes — an array is homogeneous
  • Only for numbers
  • Only when using let

Answer: Yes — an array is homogeneous. A Swift array holds elements of one type, e.g. [Int] holds only Ints.