0Pricing
Swift Academy · Lesson

Dictionary CRUD and Subscript Access

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

Dictionary CRUD and Subscript Access is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Dictionary CRUD and Subscript Access” lesson free?

Yes — the full text of “Dictionary CRUD and Subscript Access” is free to read here on the web, and the Swift Academy course includes 4 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 “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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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