Browse Questions
- @AppStorage is a property wrapper that reads and writes to UserDefaults and automatically refreshes the view when the value changes.
- No — a type's own property observers are not called while its initializer sets the property; they only fire for assignments after initialization.
- Breakpoints pause execution so you can inspect state.
- SOLID is five principles for writing maintainable, extensible code.
Answer:
@AppStorage is a property wrapper that reads and writes to UserDefaults and automatically refreshes the view when the value changes. Use it for lightweight user preferences.
Code Example:
struct SettingsView: View {
@AppStorage("isDarkMode") private var isDarkMode = false
@AppStorage("fontSize") private var fontSize = 16.0
@AppStorage("username") private var username = ""
var body: some View {
Form {
Toggle("Dark Mode", isOn: $isDarkMode)
Slider(value: $fontSize, in: 12...24)
TextField("Username", text: $username)
}
}
}
When to use vs not:
- ✅ Simple user preferences (theme, font size, onboarding seen)
- ❌ Large data, sensitive data, or complex objects — use Core Data, Keychain, or a file instead