Intermediate

Swift files, JSON, and local data

Read, write, parse, serialize, and protect data at the edges of your program.

Chapter goal: Read, write, parse, serialize, and protect data at the edges of your program.

Simple explanation

A file is long-term storage. Serialization means turning program data into a format such as JSON that can be saved or sent.

In Swift, this chapter is about moving data between memory and storage. Start with the idea above. Then connect each symbol to a value or action in the example.

Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.

Why this topic is important

Apps need persistence, imports, exports, cached settings, and clean data formats. In Swift, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.

When to use it

Use file and data handling for config files, save data, logs, reports, and offline-first features.

Example code

let profile = ["name": "Aina", "level": "3"]
print(profile["name"] ?? "")

Line-by-line explanation

What the output means

No guaranteed console output: this example prepares, saves, or converts data.

The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.

Mistake example

let profile = nil // the code assumes the file and required data always exist
print(profile["name"] ?? "")

This version intentionally shows how the code assumes the file and required data always exist. The changed assignment stores a missing value, or a required line is removed, so later code cannot complete its job safely.

Fixed version

let profile = ["name": "Aina", "level": "3"]
print(profile["name"] ?? "")

The corrected version restores the real value or required operation. It fixes the chapter-specific problem: the code assumes the file and required data always exist.

Common mistakes

Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.

Real use cases

Practice exercise

  1. Save one new record to a file.
  2. Read it back and confirm the value matches what you saved.
  3. Handle the case where the file does not exist yet.

Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.

Mini quiz

  1. Predict the output: what happens if you try to read a file before writing to it?
  2. Select the safer option: assuming a file exists, or checking before you read it?
  3. Identify complete vs. partial code: does this snippet handle a missing file?

How to read AI-generated code

Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.

Language reading check

Find the program entry point, follow function calls one at a time, and keep track of each value's type. Do not jump into a class or helper until you know who calls it.

Before you move on

Next topic

Next, learn APIs and HTTP. Before opening it, explain this chapter out loud in under one minute.

Open the interactive lesson →
← Swift debugging step by stepSwift APIs and HTTP →