-
-
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
Add @PublishedDefault for ObservableObject #128
Conversation
Thanks for working on this 👍 Have you considered integrating this into |
Maybe something like this: https://github.com/mergesort/Boutique/blob/ad47507dc0e1c4cf1693fd08567457d9caf664b7/Sources/Boutique/Stored.swift#L32-L44 |
@PublishedDefault(.opacity) var opacity | ||
} | ||
``` | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*/ | |
*/ |
Incorrect indentation on the whole comment block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, how (or what tool did you use) to adjust only the comment block's indentation width?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, on Xcode.
On Xcode /** */
block, when you hit enter key, the new line will start at 5 space position.
Did you adjust it manually every time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup...
About
|
I wonder how |
Having it be |
And you sure the subscript is not called during initialisation? |
What if in the normal |
I didn't know AppStorage become compatible with ObservableObject from iOS 14.5. (missed the detail link of reply in #48) And I finally catch up what's the problem here.
Is my understanding right?
The static subscript, Yes, it's not.
What's the |
Yes
|
Sorry I didn't get your point… What I want is a property wrapper that can trigger the owner And |
This is ugly, but one workaround may be to tell the user to do this: final class ViewModel: ObservableObject {
@PublishedDefault(.opacity) var opacity
init() {
opacity = opacity
}
} |
Thinking more about this. That is just a requirement for |
With the latest news about |
Closing this. Let's continue the discussion in #142. |
Introduction
Hi. Thanks for the awesome Defaults framework!
This PR add a new property wrapper
@PublishedDefault
, that is used exclusively forObservableObject
.@PublishedDefault
will triggerobjectWillChange
when changing the value of Defaults viaObservableObject
.You can use it like this
Detail
I used a feature called Referencing the enclosing 'self' in a wrapper type that is not in the Swift documentation but is in the Property Wrapper's Proposal. This feature makes the property wrapper can access the object that holding itself.
However, you can access the enclosing object only when that object reads/write the corresponding value, instead of all the time.
Due to this limitation,
@PublishedDefault
CANNOT observe changes in the value of the underlyingUserDefaults
, and can only triggerobjectWillChange
if the value change is made by theObservableObject
that holds it.(Well, technically you can let
@PublsihedDefaults
hold a reference to the object when read/write occurs to make the observation work. But that doesn't smell good, so I didn't try it.)It would be nice if you can also check the comments.
This PR also fix #48
Other helpful reference:
https://www.swiftbysundell.com/articles/accessing-a-swift-property-wrappers-enclosing-instance/
https://www.avanderlee.com/swift/appstorage-explained/#creating-an-alternative-solution-for-reading-and-writing-user-defaults-through-a-property-wrapper
TODO
AnyObject
as generic constraintObservableObject
if possible, instead of constrain it asObservableObject
only.@Defaults
comment and README