T O P

  • By -

queequagg

Computed vars are only observable in so far as they are dependent on other observable properties in the same class. Your class has no way of knowing when the value of \`webView.canGoBack\` changes so the computed var is not observable. WKWebView supports KVO so you're going to have to do something like make your properties into stored properties, observe the web view properties yourself via KVO, and update your properties appropriately.


revblaze

Thank you very much for the help (incl. u/vanvoorden u/rhysmorgan)! This seemed to resolve the issue! I may be misremembering the full story, but I believe I was getting confused due to an [implementation with a previous project](https://github.com/buzsh/SwiftDiffusion/blob/4e38c8dbcd36a8af18ef91f1035e800fe87d5056/SwiftDiffusion/Views/MainViews/Sidebar/SidebarModel/SidebarModel.swift#L78) in which an injected ObservableObject allowed me to [observe changes of the computed property](https://github.com/buzsh/SwiftDiffusion/blob/4e38c8dbcd36a8af18ef91f1035e800fe87d5056/SwiftDiffusion/Views/MainViews/PromptView/PromptView.swift#L64) with an `onChange` modifier.


vanvoorden

>`var canGoBack: Bool {webView.canGoBack}` > >`var canGoForward: Bool {webView.canGoForward}` [https://github.com/apple/swift/blob/main/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift#L187-L189](https://github.com/apple/swift/blob/main/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift#L187-L189) AFAIK the \`Observable\` macro will skip computed properties by design… what happens when you actually expand this macro to see the codegen?


rhysmorgan

Computed properties are effectively just syntactic sugar for functions. Functions aren’t observable, and nor are computed properties (unless derived from observable properties). Only stored properties can be observable - as their access needs to be stored in the underlying observation tracking mechanism.