Discover Observation in SwiftUI

Let’s you define models in standard swift types

What is Observation?

  • New feature for tracking changes to properties
  • Will automatically create the code for you to track and use any changes occurs via Macros. 
  • Using a computed property, when it is changed the UI updates
  • Adds performance improvements since the UI only changes when it needs to

SwiftUI Property Wrappers

  • You will still need @State if the view needs it’s own state – like a sheet presentation
  • @Environment allows you to propagate globally – Observable types work well here. 
  • @Bindable – is a lightweight just use $ syntax to create bindings.  You will need that for things like TextFields

Advanced uses

  • You can use arrays, optionals or any type that contains @Observable – it will track based on the specific instance
  • If a computed property does not have any stored properties, you need to tell observation that the property was used and when it changes, you do this via creating your own getter and setter look this up for more detail.

ObservableObject – how to update your code

  • Observable macro will simplify your code
  • Take a look at your public class <name> { } remove the @observableobject and @published, then change your view, all @Observed can be changed, mostly just delete annotations will get you there