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 initialise a @ViewModel property with a default value #19

Open
sharplet opened this issue Aug 26, 2020 · 1 comment

Comments

@sharplet
Copy link
Contributor

Because we rely on the "enclosing self" subscript to hook into a view controller's viewDidLoad method, we can't use the more obvious syntax:

@ViewModel var counter = Counter()

This is a wrinkle that it would be nice to smooth out, but it would involve complicating the implementation. A possible approach:

  1. Swizzle -[UIViewController viewDidLoad] globally at load time to post our view did load notification.
  2. Eagerly observe all viewDidLoad notifications and use reflection to find @ViewModel properties.
  3. If any are found, start observing those objects and trigger updateView().

One downside of swizzling at the UIViewController level is that the viewDidLoad notification will often get posted before a subclass's viewDidLoad implementation has returned. This could result in some tricky issues with initialisation order that aren't a problem when we can guarantee that the most-derived subclass is being swizzled.

@sharplet
Copy link
Contributor Author

Possible duplicate of #18.

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