Skip to content
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

DefaultsKit does not handle arguments passed on launch #20

Open
cfilipov opened this issue Oct 15, 2018 · 0 comments
Open

DefaultsKit does not handle arguments passed on launch #20

cfilipov opened this issue Oct 15, 2018 · 0 comments

Comments

@cfilipov
Copy link

One very useful feature when debugging is to use Arguments Passed On Launch to override user defaults. It seems DefaultsKit is not able to handle these keys because of the way it attempts to cast to Key.ValueType.

For example, let's say I have the following:

var wipeDatabaseOnLaunch = Key<Bool>("wipeDatabaseOnLaunch")

This works in the normal case:

let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: Optional<Bool>(true)

However, if we pass -wipeDatabaseOnLaunch true argument on launch, the underlying type returned by user defaults is no longer Optional<Bool> but instead Optional<Any>. Since the implementation of get tries to cast to the key's ValueType this cast will fail and you get nil instead.

let value = defaults.get(for: wipeDatabaseOnLaunch)
// value: nil

As a workaround I had to implement a specialized version of get which uses bool(forKey:)

public func get(for key: Key<Bool>) -> Bool {
    return userDefaults.bool(forKey: key._key)
}

Unfortunately, this needs to be done for each of the types you could use as launch arguments. Haven't found a cleaner solution to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant