You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-6Lines changed: 21 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -600,7 +600,19 @@ This flag helps bypass macro validation in CI environments where the full macro
600
600
601
601
### Default Value Behavior for UserDefaults and iCloud Key-Value Store
602
602
603
-
All persistent properties (those marked with @DefaultsBacked or @CloudBacked, either explicitly or implicitly) must be declared with default values. The framework captures these declaration-time defaults and maintains them as immutable fallback values throughout the object's lifetime. When keys are missing from the underlying storage (UserDefaults or iCloud Key-Value Store), properties automatically revert to these preserved default values, ensuring consistent behavior regardless of external storage modifications.
603
+
All persistent properties (those marked with @DefaultsBacked or @CloudBacked, either explicitly or implicitly) must be declared with default values. The framework captures these declaration-time defaults and maintains them as immutable model defaults throughout the object's lifetime.
604
+
605
+
Fallback order depends on the backing store:
606
+
607
+
-`@ObservableDefaults` (`UserDefaults`)
608
+
1. Persisted value in the selected `UserDefaults` domain
609
+
2. Value provided by `UserDefaults.register(defaults:)`
610
+
3. Declaration-time model default captured by ObservableDefaults
611
+
-`@ObservableCloud` (`NSUbiquitousKeyValueStore`)
612
+
1. Persisted cloud value
613
+
2. Declaration-time model default captured by ObservableDefaults
614
+
615
+
This means `removeObject(forKey:)` does not always revert to the declaration default for `UserDefaults`. If the key has a registered default, that registered default is used first.
604
616
605
617
```swift
606
618
@ObservableDefaults(autoInit:false) // @ObservableCloud(autoInit: false) is the same
@@ -625,12 +637,15 @@ let user = User(username: "alice", age: 25)
625
637
626
638
user.username="bob"// Changes current value, default value stays "guest"
print(user.age) // 18 (no registered default, so declaration default is used)
634
649
```
635
650
636
651
> **Recommendation**: Unless you have specific requirements, use `autoInit: true` (default) to generate the standard initializer automatically. This helps avoid the misconception that default values can be modified through custom initializers.
0 commit comments