UserDefaults and app preferences

6739
0

UserDefaults provides simple key-value storage for app preferences and settings. It persists basic types like strings, numbers, bools, dates, and data automatically. I use UserDefaults for user preferences, app state, and feature flags—never for sensitive data like passwords. The standard suite UserDefaults.standard is most common. App groups share defaults between apps and extensions with UserDefaults(suiteName:). SwiftUI's @AppStorage property wrapper creates reactive bindings to UserDefaults. I create type-safe wrappers with enums for keys to prevent typos. For complex objects, I encode to Data with Codable. UserDefaults syncs to disk periodically, making it reliable for non-critical data.