Swift file operations use Foundation—String 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
- Q: Sandbox paths on iOS?
A: Apps write inside container directories—Documents, Caches, tmp— not arbitrary filesystem paths. - Q: Error handling for I/O?
A: File APIs throw—usetryand typedcatchor propagate withthrows.
Self-check
- Which framework provides FileManager?
- 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.