-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support initial value for optional key #54
Comments
@hank121314 Any opinions on this? |
IMO, Although the second solution will be more user friendly, it will create an extra space storage(internal UserDefaults). I prefer the first one 😄. |
Yeah, I agree. The most common use-case is to use the standard suite anyway. |
Seems like I'm thinking we can store it in a key called The init would be: public convenience init<T>(
_ key: String,
initialValue: Value,
suite: UserDefaults = .standard
) where Value == T? {} We would only store keys coming from this initializer. |
I have:
And I would like to specify an initial value, meaning one that is used before any value is set. This is different from
default
, which is used whenever the value isnil
, which would not work here as it would make it impossible to set the value tonil
, which is the whole point of making the value optional.What I'm thinking:
The problem is that there's no good way to differentiate whether a key unset or
nil
.object(forKey:)
returnsnil
whether it's unset or set tonil
.suite.dictionaryRepresentation().keys.contains(key)
returnsnil
whether it's unset or set tonil
.suite.persistentDomain(forName:).keys.contains(key)
would work, but we only have the suite instance, not the suite name. And the suite instance doesn't expose the suite name...CFPreferencesCopyValue
returns the true value ignoring thesuite.register
defaults, but it requires a suite name.Possible solutions:
suiteName
parameter too if you want to useinitialValue
with a non-standard suite. (and useCFPreferencesCopyValue
)The text was updated successfully, but these errors were encountered: