Swift Collection Protocols

Swift Tutorial

🧩 Swift Collection Protocols – Complete Guide (Beginner → Advanced → Interview)

Swift’s collections aren’t just types like Array, Set, and Dictionary—they are built on powerful protocols.
Understanding these protocols helps you write generic, reusable, and high-performance code and is a favorite interview topic.


1️⃣ What are Collection Protocols in Swift? ⭐

Collection protocols define common behavior for data structures that hold multiple values.

They let you:

  • Write generic algorithms

  • Work with any collection type

  • Get performance guarantees


2️⃣ Core Collection Protocol Hierarchy ⭐⭐⭐

Sequence
└── Collection
├── BidirectionalCollection
│ └── RandomAccessCollection
└── MutableCollection

📌 Array, Set, Dictionary conform to these (directly or indirectly).


3️⃣ Sequence Protocol ⭐

The most basic collection protocol.

Key Points

  • Can be iterated once

  • Supports for-in, map, filter, reduce

  • No indexing guarantee

Example


 

📌 Many things are sequences (even generators).


4️⃣ Collection Protocol ⭐⭐

Adds multi-pass iteration and safe indexing.

Key Features

  • startIndex, endIndex

  • Subscript access

  • Guaranteed multiple iterations

Example


 


5️⃣ BidirectionalCollection ⭐⭐

Allows traversal both forward and backward.

Key Feature

  • index(before:)

Example


 

Output

30

📌 Array, String, ArraySlice conform to this.


6️⃣ RandomAccessCollection ⭐⭐⭐

Provides O(1) index movement (fastest access).

Why It Matters

  • Enables high-performance algorithms

  • Sorting/searching are faster

Example

📌 Array conforms
📌 LinkedList would NOT


7️⃣ MutableCollection ⭐⭐

Allows modifying elements in place.

Example


 

Output

[10, 2, 3]

📌 Requires var, not let.


8️⃣ RangeReplaceableCollection ⭐⭐⭐

Allows adding/removing elements.

Key Methods

  • append

  • insert

  • remove

  • replaceSubrange

Example


 

Output

[2, 3, 4]

📌 Array, String conform to this.


9️⃣ LazySequenceProtocol ⭐⭐⭐ (Performance)

Used when calling .lazy.

Example


 

📌 Stops processing as soon as result is found.


🔟 Dictionary-Specific Protocols ⭐⭐

  • Dictionary.KeysCollection

  • Dictionary.ValuesCollection


 


1️⃣1️⃣ Writing Generic Code Using Collection Protocols ⭐⭐⭐


 

📌 Works with:

  • Array

  • Set

  • Dictionary values

  • String


1️⃣2️⃣ Performance Guarantees (Interview Gold) ⭐⭐⭐

Protocol Index Movement
Collection O(n)
BidirectionalCollection O(1) backward
RandomAccessCollection O(1) forward & backward

📌 Choosing the right protocol matters.


1️⃣3️⃣ Common Mistakes ❌

❌ Using Sequence when multiple passes are needed
❌ Assuming all collections are random-access
❌ Ignoring lazy for large datasets
❌ Over-constraining generics


📌 Interview Questions (Swift Collection Protocols)

Q1. Difference between Sequence and Collection?
👉 Collection supports multiple passes & indexing

Q2. Why is RandomAccessCollection important?
👉 Performance guarantees

Q3. Which protocol allows mutation?
👉 MutableCollection

Q4. Which allows insertion/removal?
👉 RangeReplaceableCollection


✅ Summary

✔ Swift collections are protocol-based
Sequence → basic iteration
Collection → indexed, multi-pass
BidirectionalCollection → reverse traversal
RandomAccessCollection → fastest access
MutableCollection → modify elements
RangeReplaceableCollection → add/remove
✔ Essential for generic & high-performance Swift

You may also like...