Swift Academy · Lesson

Sequence/Collection hierarchies

Understand Sequence vs Collection (and Bidirectional/RandomAccess): traversal guarantees, indices, and when algorithms require stronger protocols.

Lesson 1 of 38 steps

Sequence/Collection hierarchies is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

Hierarchy map

The iterable stack:

  • Sequence: iteration via iterator (may be single-pass).
  • Collection: multi-pass, indexable (startIndex/endIndex).
  • BidirectionalCollection: can move indices backward.
  • RandomAccessCollection: constant-time index steps.

Sequence iteration

Sequence guarantees you can iterate elements in order; nothing about indices or multi-pass is required.

let seq = [1, 2, 3] as AnySequence<Int>    // wrap as AnySequence for demo
for x in seq { print(x, terminator: " ") }
print("")

Collection indices

Collection adds indices and allows multi-pass traversal. You can re-iterate safely.

let arr = ["a","b","c"]          // Array is a Collection
print(arr.startIndex == 0)            // true for Array
print(arr.endIndex == 3)              // endIndex is "one past last"
for i in arr.indices { print(arr[i], terminator: " ") }
print("")
// Iterate again (multi-pass)
for ch in arr { print(ch, terminator: " ") }
print("")

Stronger capabilities

BidirectionalCollection can move indices backward. RandomAccessCollection supports constant-time index steps (e.g., Array).

let text = "Swift"                         // String is a BidirectionalCollection
var i = text.index(before: text.endIndex)       // move backward
print(text[i])                                   // last character
// Array is RandomAccessCollection: jumping indices is cheap
let nums = [10,20,30,40]
print(nums[nums.startIndex.advanced(by: 2)])     // 30

Custom Sequence

A custom Sequence may be single-pass. After iterating once, the iterator can be exhausted.

struct Countdown: Sequence, IteratorProtocol {
    var current: Int
    mutating func next() -> Int? {
        guard current >= 0 else { return nil }
        defer { current -= 1 }
        return current
    }
}
var cd = Countdown(current: 3)
for n in cd { print(n, terminator: " ") }   // 3 2 1 0
print("")
// cd is exhausted; iterating again yields nothing (single-pass behavior)

Pick the right protocol

Guidelines:

  • Use generic algorithms on Sequence when one pass is enough.
  • Require Collection for multi-pass or index ops.
  • Prefer RandomAccessCollection for algorithms that jump indices often (e.g., binary search).

Collection vs Sequence

Quick check: What does Collection add over Sequence?

Recap

Recap: Sequence → basic iteration. Collection → indices + multi-pass. Bidirectional → move backward. RandomAccess → constant-time index steps. Choose the smallest protocol that fits your algorithm.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Sequence/Collection hierarchies” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Sequence/Collection hierarchies”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

What will I learn in “Sequence/Collection hierarchies”?

Understand Sequence vs Collection (and Bidirectional/RandomAccess): traversal guarantees, indices, and when algorithms require stronger protocols. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “Sequence/Collection hierarchies” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Sequence/Collection hierarchies
  2. Map/Filter/Reduce, lazy sequences
  3. Custom iterators & performance tips
← Back to Swift Academy