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

It should be possible to init the property wrappers inline #18

Open
helje5 opened this issue Aug 26, 2020 · 0 comments
Open

It should be possible to init the property wrappers inline #18

helje5 opened this issue Aug 26, 2020 · 0 comments

Comments

@helje5
Copy link

helje5 commented Aug 26, 2020

Instead of

@ViewModel private var counter: Counter

required init?(coder: NSCoder) {
  super.init(coder: coder)
  self.counter = Counter()
}

a simple:

@ViewModel private var counter = Counter()

It doesn't work yet, because it uses the subscript hack to get access to self: https://github.com/thoughtbot/CombineViewModel/blob/main/Sources/CombineViewModel/ViewModel.swift#L20

This magic subscript gets called instead of wrappedValue, and the “enclosing self” gets passed in

I would try to make it a DynamicProperty in SwiftUI terms, the question is how and when (w/o requiring a special subclass). Maybe using some NSNotification.

It’s possible that I could use a more global swizzle of viewDidLoad (or some other event) and then use reflection to find the ViewModel/ObservedObject and register it lazily

I think 5.3 has that new _ reflection function which might make the Reflection library unnecessary. Didn't look at it yet

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