Comments (6)
from foil.
This is awesome, thanks so much @jessesquires. Sorry I failed to send the PR!
from foil.
Hi @jgale! 👋🏼 Thanks for this!
I would happily accept a PR for this.
Maybe if there was an
optional init?(storedValue: StoredValue)
in the protocol?
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 4.x
release, as it would not require any breaking changes.
If the protocol instead had a failable initializer like
init?(storedValue:)
it would work fine.
This works too, and I think is a slightly better solution, but a breaking change and requires a 5.0
release.
My only concern is that I don't want this to impact usage of the non-optional @WrappedDefault
. Users shouldn't have to worry about optionals in this scenario.
Regarding planning / releasing — assuming both of the above are possible, I would prefer to do a non-breaking 4.x
release first and then follow-up with a new 5.0
release with the better API.
Another thought (maybe for 5.0
) is just providing direct support for Codable
types, similar to all the existing "built-in" types. (Honestly, I should just give in and do this because I know folks want it. 😅 So we might as well implement it correctly and unit test it so that users don't have to do this.)
from foil.
Thanks for the reply @jessesquires! I played around with making these changes in Foil today. I couldn't get the optional init?(storedValue: StoredValue)
to work because optional
only works for Objective-C protocols, which we don't want. I couldn't come up with any other way to make it optional.
Then I tried converting the signature to init?(storedValue: StoredValue)
. This works very well for my goals, and has surprising (to me) effects. Most of the extensions that implement UserDefaultsSerializable
still compile and work without changes! e.g. Bool
/Int
/URL
/Data
/others. There must be something I don't understand about Swift initializers where init(storedValue: StoredValue)
matches the protocol requirement for init?(storedValue: StoredValue)
. Some of the ones do need to change, e.g. Array
/Set
/Dictionary
/RawRepresentable
. The changes are very simple, usually moving from map
to compactMap
.
So this would break some client code implements UserDefaultsSerializable
, though I don't fully understand under what circumstances. Presumably that implies it should be a 5.0
change like you said.
These changes have an added benefit, IMO. For example, let's say you want to store an optional enum TestEnum: String
using Foil. If there is an invalid rawValue
string stored in UserDefaults, when you attempt to read it, you'd now get back nil
instead of it blowing up in the force-unwrap in the UserDefaultsSerializable
extension that implements RawRepresentable
support.
I am totally fine with waiting for 5.0
since I can't come up with any other way to make it optional. Would you like me to send a PR with the changes?
Also, I'd be happy to have full-on Codable
support in 5.0
. 😁
from foil.
I couldn't get the
optional init?(storedValue: StoredValue)
to work becauseoptional
only works for Objective-C protocols, which we don't want. I couldn't come up with any other way to make it optional.
Ahh yes. I knew there was something here, but I couldn't think of it the other day.
There must be something I don't understand about Swift initializers where
init(storedValue: StoredValue)
matches the protocol requirement forinit?(storedValue: StoredValue)
.
Hm... that's interesting. I'll dig around and see what I can find out.
The changes are very simple, usually moving from
map
tocompactMap
.
👍🏼
Presumably that implies it should be a
5.0
change like you said.
Yes, we'll just do this to be safe and folks and migrate over when they want.
These changes have an added benefit, IMO. For example, let's say you want to store an optional
enum TestEnum: String
using Foil. If there is an invalidrawValue
string stored in UserDefaults, when you attempt to read it, you'd now get backnil
instead of it blowing up in the force-unwrap in theUserDefaultsSerializable
extension that implementsRawRepresentable
support.
Nice. 😎
Would you like me to send a PR with the changes?
Please do!
Also, I'd be happy to have full-on
Codable
support in5.0
. 😁
That sounds great! Let's do this one in a separate PR. 😄
from foil.
@jgale no worries! no apology necessary!
from foil.
Related Issues (14)
- Support more types by default. Expand UserDefaultsSerializable to support Int8, Int16, NSArray, NSNumber, etc. HOT 7
- Using UserDefaults Publisher? HOT 2
- Drop CocoaPods support? HOT 3
- Lower deployment target HOT 9
- Deployment targets inconsistency between Pod and SPM metafiles HOT 1
- [Suggestion] Succinct implicit initialization HOT 1
- [Bug]: user defaults returns outdated value after rebuilding project HOT 2
- Prepare and release 3.0
- [Feature]: A demo for SwiftUI project HOT 4
- [Feature]: Expose a `Binding` for SwiftUI if possible HOT 6
- Xcode 14 + infra updates + 4.0 release prep
- Rename "WrappedDefault" and "WrappedDefaultOptional"? HOT 3
- Setting non-nil String value crashes on force-unwrapping fetchOptional? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foil.