-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
[Feature]: Be able to save Codable
structs (or a workaround?)
#72
Comments
Hi @jgale! 👋🏼 Thanks for this! I would happily accept a PR for this.
I think this is possible, but I would have to check if the compiler complains about anything. This would be a good solution for a
This works too, and I think is a slightly better solution, but a breaking change and requires a My only concern is that I don't want this to impact usage of the non-optional Regarding planning / releasing — assuming both of the above are possible, I would prefer to do a non-breaking Another thought (maybe for |
Thanks for the reply @jessesquires! I played around with making these changes in Foil today. I couldn't get the Then I tried converting the signature to So this would break some client code implements These changes have an added benefit, IMO. For example, let's say you want to store an optional I am totally fine with waiting for Also, I'd be happy to have full-on |
Ahh yes. I knew there was something here, but I couldn't think of it the other day.
Hm... that's interesting. I'll dig around and see what I can find out.
👍🏼
Yes, we'll just do this to be safe and folks and migrate over when they want.
Nice. 😎
Please do!
That sounds great! Let's do this one in a separate PR. 😄 |
This is awesome, thanks so much @jessesquires. Sorry I failed to send the PR! |
@jgale no worries! no apology necessary! |
Guidelines
Description
Hello! Big Foil fan here. :)
I'd like to be able to save a
Codable
struct in UserDefaults using Foil. I know this is somewhat controversial, and "highly discouraged" but I'm aware of the tradeoffs and I'm OK with it. I'm open to any possible way of doing that, even if it's not a feature that's added to Foil itself.Problem
Be able to save and retrieve small Codable structs from UserDefaults.
The problem I'm running into is that I want the decoding to fail gracefully if the struct can't be decoded, but I can't figure out how to do that.
I have a
MyCodableStruct
the conforms toCodable
. I have exposed it in myAppSettings
like so:I've tried to have my type conform to
UserDefaultsSerializeable
via the following approach:This actually works, and I can save/restore the struct. However, I want it to be able to fail gracefully if there is some garbage stored in that string. Currently it hits the fatal error above. I would like it to just return
nil
if the decoding fails, but that's not an option in this initializer.Proposed Solution
I'm a little out of my depth with property wrappers and generics, so I'm not sure how to do this.
What causes it to crash is the last line of
fetchOptional<T>(_ key: String) -> T?
where it doesreturn T(storedValue: storedValue)
.If the protocol instead had a failable initializer like
init?(storedValue:)
it would work fine. It could return nil, which I would be happy with. Maybe if there was anoptional init?(storedValue: StoredValue)
in the protocol? Or aUserDefaultsSerializableOptional
?Alternatives Considered
I'm open to any other ideas as well. Thanks for your help and your great work!
The text was updated successfully, but these errors were encountered: