Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

file-io-swift

File I/O

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches File I/O: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship File I/O in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply File I/O in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

Toward the end of the track—consolidate before capstone-style review lessons.

Swift file operations use FoundationString reading/writing, FileManager, and URL-based paths. Sandbox lessons demonstrate APIs with print(); full filesystem access runs locally.

Reading and writing strings

import Foundation

let text = "Hello, file"
let url = URL(fileURLWithPath: "/tmp/demo.txt")
try text.write(to: url, atomically: true, encoding: .utf8)
let read = try String(contentsOf: url)

FileManager

Check existence, create directories, enumerate files—prefer URL APIs over legacy NSString paths.

Important interview questions and answers

  1. Q: Sandbox paths on iOS?
    A: Apps write inside container directories—Documents, Caches, tmp— not arbitrary filesystem paths.
  2. Q: Error handling for I/O?
    A: File APIs throw—use try and typed catch or propagate with throws.

Self-check

  1. Which framework provides FileManager?
  2. Why do file APIs use throws?

Tip: iOS apps write inside sandbox containers—Documents, Caches, and tmp—not arbitrary paths.

Interview prep

iOS sandbox?

Apps write inside container directories—not arbitrary filesystem paths.

I/O errors?

File APIs throw—handle with try/catch.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • FileManager use?
  • Data vs String read?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump