Adding the @Observable macro
The primary goal of the Observation framework is to simplify our work as much as possible, and it does that with the heavy use of macros.
Let’s take a Book
class, for example:
class Book: ObservableObject { @Published var title:String = "" @Published var author: String = "" @Published var publishedYear: Date = Date() @Published var numberOfPages: Int = 0 }
The Book
class is a standard ObservableObject
class that contains four properties with a @Published
property wrapper.
Using the Observation
framework, we can get rid of all the property wrappers and the ObservableObject
protocol and just add a macro attached to the class declaration:
@Observable class Book { var title:String = "" var author: String = "" var publishedYear: Date...