Migrating from ObservableObject to Observable in SwiftUI but still run on older OS

I prepare an app to migrate from ObservableObject to Observable, from EnvironmentObject to Environment(MyClass.self) and so so forth.

That works OK, very simple.

But, that forces to run on macOS 14 or more recent.

So I would like to have it conditionally, such as:

  • if macOS 14 available
    @Environment(ActiveConfiguration.self) var activeConfiguration: ActiveConfiguration
  • otherwise
    @EnvironmentObject var activeConfiguration: ActiveConfiguration 

The same for the class declaration:

  • if macOS 14 available
@Observable class ActiveConfiguration {
    var config = Configuration()
}
  • otherwise
class ActiveConfiguration : ObservableObject {  
   @Published var config = Configuration()
}

Is there a way to achieve this (I understand it is not possible through extensions of Macros, as we can do for modifiers) ?

Could I define 2 classes for ActiveConfiguration, but then what about @Environment ?

Have a look at the Perception package from the point.free guys. It is a backport of the Observation tools to earlier OSs:

https://github.com/pointfreeco/swift-perception

Migrating from ObservableObject to Observable in SwiftUI but still run on older OS
 
 
Q