Pick the fix
A draft must never be corrupted by a crash mid-write, and must be encrypted while the device is locked. Which write?
Hint 1 of 1
Two requirements: atomic and protected.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A try data.write(to: url, options: [.atomic, .completeFileProtection])
Correct. It writes to a temporary file, swaps it in, and protects it.
Why not the others?
- B
try data.write(to: url). No atomicity, so a crash can leave a partial file. - C
FileManager.default.createFile(atPath:contents:)every second. It's not atomic or protected, and wastes I/O. - D Save it in UserDefaults. It's not for documents and isn't protected.