Skip to content

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

@helje5

Description

@helje5

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions