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:
- Swizzle
-[UIViewController viewDidLoad] globally at load time to post our view did load notification.
- Eagerly observe all
viewDidLoad notifications and use reflection to find @ViewModel properties.
- 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.
Because we rely on the "enclosing self" subscript to hook into a view controller's
viewDidLoadmethod, we can't use the more obvious syntax:This is a wrinkle that it would be nice to smooth out, but it would involve complicating the implementation. A possible approach:
-[UIViewController viewDidLoad]globally at load time to post our view did load notification.viewDidLoadnotifications and use reflection to find@ViewModelproperties.updateView().One downside of swizzling at the
UIViewControllerlevel is that theviewDidLoadnotification will often get posted before a subclass'sviewDidLoadimplementation 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.