Introducing ObservableConverter
A plugin to help convert to @Observable
At WWDC 2023, Apple introduced the brand new @Observable
macro, a simple way to define a type with properties that will automatically update SwiftUI views when they change, replacing the ObservableObject
protocol and @Published
properties. This new feature is great, but what should we do with all of our old types that use ObservableObject
and outdated property wrappers like @StateObject
, @ObservedObject
, and @EnvironmentObject
? To kickstart this conversion in Xcode, we at Lickability started writing a small tool and are releasing the first beta today: ObservableConverter.
ObservableConverter is a Swift command plugin that, when installed from Xcode 15, makes converting from ObservableObject
to using @Observable
literally just a few clicks after adding the dependency. Select your target in the project navigator, right click, and select Convert Target to Use @Observable. It will update your ObservableObject
classes, convert all the old property wrappers, and make some view modifier updates. From the plugin’s executable, we make use of Apple’s SwiftSyntax
to get a view of the syntax tree for each file and make some safe replacements and modifications. Once you run the conversion, you can simply remove the dependency entirely!
The tool is still very much in beta, so while it covers converting from the use cases Apple covered in Discover Observation in SwiftUI, there are many more nuanced and advanced cases that will require making some decisions on what’s best for your code. We plan to support some of those cases in the future, but for now we hope this is a nice place to start when adopting the new @Observable
macro! Feel free to file feature requests or bugs.