Chapter 63: Swift Collections

The Big Picture: What are Collections and Why Three Main Types?

A collection in Swift is any type that holds zero or more values.

Swift gives you three main choices for everyday work — each solves a different problem:

Collection Type Ordered? Allows duplicates? Fast lookup by value? Fast lookup by key? Most common real-life feeling Typical use case examples
Array Yes Yes No (slow) No “I have a list of things in a specific order” shopping cart, to-do list, recent searches, messages
Set No No Yes (very fast) No “I have a group of unique items — order doesn’t matter” tags, permissions, unique IDs, friends list (no duplicates)
Dictionary No (insertion order since Swift 4.2) Yes (different keys) No Yes (very fast) “I have items and I want to find them by a key/label” user profile, JSON data, settings, lookup tables

Golden rule you should burn into your mind:

  • Need order or duplicates → almost always Array
  • Need uniqueness and fast “does it contain X?”Set
  • Need fast lookup by key (“give me value for this name/ID”) → Dictionary

Chapter 2 – Arrays (the most common collection – you will use this 70–80% of the time)

2.1 Creating & basic operations

Swift

2.2 Real-life example – shopping cart / order summary

Swift

Chapter 3 – Sets (when uniqueness matters)

3.1 Creating & basic operations

Swift

Real-life example – unique permissions / roles

Swift

Chapter 4 – Dictionaries (lookup by key)

4.1 Creating & basic operations

Swift

Real-life example – user profile / settings

Swift

Modern style (strongly typed dictionary)

Swift

Chapter 5 – Quick Comparison Table (keep this in mind)

You want to… Use this collection Typical operations Example real-life case
Keep order, allow duplicates Array append, insert(at:), remove(at:), enumerated() shopping cart, task list, chat messages
Ensure uniqueness, fast “contains?” Set insert, contains, remove, union/intersection tags, permissions, unique IDs, friends (no dupes)
Fast lookup by key Dictionary [key] = value, updateValue(forKey:), removeValue user profile, settings, JSON data, lookup tables

Chapter 6 – Small Practice – Try these

  1. Create an array of 5 favorite foods → Print them numbered → Add one more at the beginning → Remove the last one
  2. Create a set of 6 tags (some duplicates) → Add 2 more → Check if “Swift” is present → Print all unique tags
  3. Create a dictionary for a user profile → name, age, city, isPremium → Print all key-value pairs → Change age and add favoriteColor

Paste your code here if you want feedback or want to see more polished versions!

What would you like to explore next?

  • Array slicing & memory behavior with slices
  • Sorting arrays (simple & custom comparators)
  • Filtering, mapping, reducing in depth
  • Dictionaries in more depth (typed keys, default values)
  • Sets operations (union, intersection, difference)
  • Or move to another topic (optionals, switch, functions…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *