5 concepts · 10 practice questions
UserDefaults is for small settings and flags; it is not a secure store and is read into memory, so it suits neither secrets nor large data. The Keychain is for secrets such as tokens and passwords. Files go in Application Support for data the app needs, in Caches for anything you can download again (the system may purge it when storage is low), and in Documents only for files the user should see. Write files atomically, and set a data protection level so they stay encrypted while the device is locked.
UserDefaults.standard.set(true, forKey: "hasSeenOnboarding") // a small setting
let support = try FileManager.default.url(for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil, create: true)
try data.write(to: support.appending(path: "drafts.json"),
options: [.atomic, .completeFileProtection])
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
// Caches: the system may clear it when storage is low
Sign in to read the rest and take the 10-question test.
Sign in to continue