Skip to content
Prepare to Grow

Swift Fundamentals

Arrays, Sets, and Dictionaries

Store groups of values using collection types chosen for order, uniqueness, and lookup.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Choose the appropriate Swift collection.
  • Read, add, update, and remove elements.
  • Iterate without unsafe indexing.
  • Understand empty collection syntax and type inference.
01

Learning stage

Concept Explanation

Swift provides Array for ordered values, Set for unique hashable values, and Dictionary for key-value associations. All elements in a collection share compatible types.

Collection choice communicates requirements. Choose based on whether order, duplicate values, membership checks, or keyed lookup matter to the feature.

  • Array preserves ordered positions and allows duplicates.
  • Set stores unique Hashable values without a meaningful positional index.
  • Dictionary associates unique Hashable keys with values.
  • isEmpty and count describe collection size safely.
02

Learning stage

Syntax & Examples

The example uses each collection for a different requirement.

LearningCollections.swiftswift
var lessonOrder = ["Types", "Functions", "Optionals"]
var completed: Set<String> = ["Types"]
var minutesByLesson = ["Types": 12, "Functions": 13]

lessonOrder.append("Collections")
completed.insert("Functions")
minutesByLesson["Optionals"] = 15

for lesson in lessonOrder {
  let status = completed.contains(lesson) ? "Done" : "Next"
  print("\(lesson): \(status)")
}
The array controls order, the set answers membership, and the dictionary stores keyed durations.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Arrays power ordered feeds, navigation histories, and lists.
  • Sets track selected identifiers, permissions, and unique tags.
  • Dictionaries index cached objects, configuration, and grouped results by meaningful keys.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Accessing an array index without confirming it is valid.
  • Using an array for frequent membership tests when a set expresses uniqueness better.
  • Assuming dictionary iteration has a business-significant order.
  • Mutating a collection while relying on indices captured before the mutation.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Choose the collection

Choose Array, Set, or Dictionary for a playlist order, selected category IDs, and a user lookup by ID. Explain why.

Hint: Ask whether order, uniqueness, or keyed retrieval is the primary need.

Activity 2

Handle an empty array

Create a function that returns the first lesson title or “No lessons” without indexing the array directly.

Hint: The first property returns an optional.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Which collection guarantees unique elements?

  1. A.Array
  2. B.Set
  3. C.Dictionary values
  4. D.Tuple
Reveal answer

Set

A set stores each Hashable element at most once.

QUESTION 2

What does dictionary[key] return?

Reveal answer

An optional value, because the key might not exist.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

How do you choose between Array, Set, and Dictionary?

Short answer: Use Array for order, Set for unique membership, and Dictionary for lookup by key.

Explore the detailed answer

The decision should follow the operation and invariant the model needs. Arrays provide positional order, sets express uniqueness and efficient membership, and dictionaries associate keys with values. Performance matters, but semantic intent comes first.

Possible follow-ups

  • • Why does dictionary lookup return an optional?
  • • When is a Set preferable to an Array?
08

Learning stage

Key Takeaways

Collections are not interchangeable containers. Pick the type whose guarantees match the problem, and use safe APIs instead of assuming an index or key exists.

  • Array communicates order.
  • Set communicates uniqueness.
  • Dictionary communicates key-based association.
  • Potentially missing elements are represented with optionals.