CombineViewModel icon indicating copy to clipboard operation
CombineViewModel copied to clipboard

It should be possible to init the property wrappers inline

Open helje5 opened this issue 5 years ago • 0 comments

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

helje5 avatar Aug 26 '20 01:08 helje5