I am sorry if I missed this (I haven't had a chance to read through all the posts above, so maybe this is out of scope or unsupported) but I was wondering if it would be possible to assign a shared value to all instances declared in a given type? For example, I might have a @Preference
property wrapper and I want to inject an instance of some type that conforms to my PreferencesType
protocol into all instances of @Preference
in a given type. Right now, you need to inject it into each property wrapper manually, something like:
struct S {
@Preference(key: "some_key_1") var key1: Bool
@Preference(key: "some_key_2") var key2: Int
@Preference(key: "some_key_3") var key3: String
init(defaults: PreferencesType = UserDefaults.shared) {
self._key1 = .init(defaults: defaults)
self._key2 = .init(defaults: defaults)
self._key3 = .init(defaults: defaults)
}
// Alternatively
init(defaults: PreferencesType = UserDefaults.shared) {
self.defaults = defaults
configureWrappers()
}
private func configureWrappers() {
$key1.defaults = defaults
$key2.defaults = defaults
$key3.defaults = defaults
}
}
It would be nice if you could do something like this instead:
// Assuming I have declared @shared var defaults: UserDefaultsProtocol? in my property wrapper
init(defaults: UserDefaultsProtocol = UserDefaults.shared) {
self.$preferencesShared.defaults = defaults
}
Effectively, I want to inject a value (provided to me via an initializer) into all instances of the property wrapper within a given type (and not globally i.e. into every instance throughout the app) to avoid having to write a lot of boilerplate.