Sequence/Collection hierarchies
Understand Sequence vs Collection (and Bidirectional/RandomAccess): traversal guarantees, indices, and when algorithms require stronger protocols.
Sequence/Collection hierarchies is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)]) // 30Custom 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.
Frequently asked questions
Is the “Sequence/Collection hierarchies” lesson free?
Yes — the full text of “Sequence/Collection hierarchies” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
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
- Sequence/Collection hierarchies
- Map/Filter/Reduce, lazy sequences
- Custom iterators & performance tips