@FoilDefaultStorage for non-optional values and @FoilDefaultStorageOptional for optional ones. You may wish to store all your user defaults in one place, however, that is not necessary. Any property on any type can use this wrapper.enum keysenum for the keys, writing an extension specific to your app is easy. However, this is not required. In fact, unless you have a specific reason to reference the keys, this is completely unnecessary.NSObject and the property must be declared as @objc dynamic.average does not need the @objc dynamic annotation, .receiveValue will fire immediately with the current value of average and on every change after.userId needs the @objc dynamic annotation and AppSettings needs to inherit from NSObject. Then receiveValue will fire only on changes to wrapped object's value. It will not publish the initial value as in the example above.@FoilDefaultStorage.UserDefaultsSerializable protocol defines a failable initializer, init?(storedValue:), it is possible to provide a custom implementation with a non-failable initializer, which still satisfies the protocol requirements.Bool, Int, Double, String, etc.), the default implementation of UserDefaultsSerializable is non-failable.UserDefaultsSerializable. However, this is highly discouraged as all plist types are supported by default. UserDefaults is not intended for storing complex data structures and object graphs. You should probably be using a proper database (or serializing to disk via Codable) instead.Foil supports storing Codable types by default, you should use this sparingly and only for small objects with few properties.BoolIntUIntFloatDoubleStringURLDateDataArraySetDictionaryRawRepresentable typesCodable typesCodable typesCodable types and using the default implementation of UserDefaultsSerializable provided by Foil, then you must use the optional variant of the property wrapper, @FoilDefaultStorageOptional. This will allow you to make breaking changes to your Codable type (e.g., adding or removing a property). Alternatively, you can provide a custom implementation of Codable that supports migration, or provide a custom implementation of UserDefaultsSerializable that handles encoding/decoding failures. See the example below.RawRepresentable typesRawRepresentable types, especially as properties of a Codable type require special considerations. As mentioned above, Codable types must use @FoilDefaultStorageOptional out-of-the-box, unless you provide a custom implementation of UserDefaultsSerializable. The same is true for RawRepresentable types.RawRepresentable types must use @FoilDefaultStorageOptional in case you modify the cases of your enum (or otherwise modify your RawRepresentable with a breaking change). Additionally, RawRepresentable types have a designated initializer that is failable, init?(rawValue:), and thus could return nil.Codable type that has RawRepresentable properties, by default those properties should be optional to accommodate the optionality described above.RawRepresentable types, you can provide a non-failable initializer:LICENSE for details.Posted Aug 12, 2025
Developed Foil, a property wrapper for UserDefaults in Swift.
0
2