George
George

Reputation: 793

New @Observable Macro: Should private properties be @ObservationIgnored? (iOS 17+)

All articles I read put @ObservationIgnored for private properties. However, Apple in their Focus Cookbook sample project, didn't put @ObservationIgnored to private properties.

Upvotes: 2

Views: 943

Answers (2)

antfarm
antfarm

Reputation: 155

Apple's documentation [1] says

Observation doesn’t require a property wrapper to make a property observable. Instead, the accessibility of the property in relationship to an observer, such as a view, determines whether a property is observable.

If you have properties that are accessible to an observer that you don’t want to track, apply the ObservationIgnored() macro to the property.

[1] https://developer.apple.com/documentation/swiftui/migrating-from-the-observable-object-protocol-to-the-observable-macro

Upvotes: 1

ingconti
ingconti

Reputation: 11666

my two cents... I think a decent explanation is on ADC site:

https://forums.developer.apple.com/forums/thread/763664

I found interesting the effect of "private".

Useful for others:

  • "@Observable" macro cannot be prepended to computed vars: You got:

    'extension' macro cannot be attached to property ('....')

  • seems to me weird.. but is allowed to prepend "@ObservationIgnored " to computed property without warnings / errors. (maybe wanted..)

Upvotes: 0

Related Questions