Swift Academy · Lesson

Dictionary CRUD and Subscript Access

Creating, reading, updating and deleting key-value pairs safely.

Lesson 1 of 412 steps

Dictionary CRUD and Subscript Access is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. 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 4 lessons in total.

Welcome

Swift Dictionaries map unique keys to values. They offer O(1) average-case lookup, making them ideal for keyed data access. Let's master CRUD operations.

Creating Dictionaries

```swift var empty: [String: Int] = [:] var ages = ["Alice": 30, "Bob": 25] var inferred = Dictionary(uniqueKeysWithValues: zip(["a","b"],[1,2])) ```

Reading Values Safely

```swift let score = ages["Alice"] // Optional(30) let miss = ages["Charlie"] // nil if let age = ages["Bob"] { print("Bob is \(age)") } ``` Subscript access returns an Optional — the key may not exist.

Default Value Subscript

```swift print(ages["Charlie", default: 0]) // 0 ages["Dave", default: 0] += 1 // inserts Dave:1 ``` `default:` avoids force unwrapping and enables in-place increments.

Inserting and Updating

```swift var d = ["a": 1] d["b"] = 2 // insert d["a"] = 10 // update d.updateValue(20, forKey: "a") // returns old Optional(10) ```

Removing Entries

```swift var d = ["a":1,"b":2,"c":3] d["b"] = nil // removes key "b" d.removeValue(forKey:"c") // returns Optional(3) d.removeAll() // empty ```

keys and values

```swift let d = ["x":10,"y":20] print(Array(d.keys)) // ["x","y"] (unordered) print(Array(d.values)) // [10,20] ``` `keys` and `values` are lazy collection views, not arrays.

Merging Dictionaries

```swift var base = ["a":1,"b":2] let extra = ["b":20,"c":3] base.merge(extra) { _, new in new } // keep new on conflict // ["a":1,"b":20,"c":3] ```

Counting and isEmpty

```swift let d = ["a":1,"b":2] print(d.count) // 2 print(d.isEmpty) // false ```

contains(where:) on Dictionaries

```swift let scores = ["Alice":85,"Bob":92] let hasHighScore = scores.contains { $0.value >= 90 } print(hasHighScore) // true ```

Quick Check

What does `dict["key"]` return in Swift?

Recap

Key takeaways: • Subscript returns `Value?` — always handle the optional • `dict[key, default: v]` — inline fallback • `updateValue(_, forKey:)` — returns old value • `dict[key] = nil` removes the entry • `merge(_:uniquingKeysWith:)` combines dictionaries Next: iterating and transforming dictionaries.
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 “Dictionary CRUD and Subscript Access” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Dictionary CRUD and Subscript Access”, 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 4 lessons in total.

What will I learn in “Dictionary CRUD and Subscript Access”?

Creating, reading, updating and deleting key-value pairs safely. 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 4.

How long does the “Dictionary CRUD and Subscript Access” 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. Dictionary CRUD and Subscript Access
  2. Iterating and Transforming Dictionaries
  3. Set Operations: Union, Intersection, Difference
  4. Choosing Between Array, Dictionary and Set
← Back to Swift Academy