-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support @Observable macro #120
Comments
This is an issue with @observable. "In the current implementation, the @observable macro requires that all stored properties have a default value. This helps observable types rely on definitive initialization, use the implicitly generated initializers, and define additional initializers in extensions." You should, however, be able to get past the issue by doing something like... @ObservationIgnored @Injected(\.loginService) private var loginService: LoginServicing Also keep in mind that if you use @observable your application can only run on iOS 17 and higher. |
I did figure this out, even before seeing this, but I'm seeing a new issue that I wasn't having with struct WeatherAgeFooterView_Previews: PreviewProvider {
static var previews: some View {
let _ = Container.shared.weatherManager.register { WeatherManagerPreview() }
WeatherAgeFooterView()
}
} is no longer working. In all my previews, I now get errors because they are running the Impl objects in my EDIT: I'd also be interested in a documentation section on how to get Factory injection like the above working with |
I think your problem is elsewhere, as the following works... import Factory
#if canImport(Observation)
import Observation
#endif
import SwiftUI
@available(iOS 17, *)
protocol ObservationServiceType: AnyObject {
var name: String { get set }
}
@available(iOS 17, *)
@Observable
class ObservationService: ObservationServiceType {
var name: String = "ObservationService"
}
@available(iOS 17, *)
@Observable
class MockObservationService: ObservationServiceType {
var name: String = "MockObservationService"
}
@available(iOS 17, *)
extension Container {
var observableService: Factory<ObservationServiceType> {
self { ObservationService() }
}
}
@available(iOS 17, *)
struct ObservableView: View {
@Injected(\.observableService) var observableService
var body: some View {
HStack {
Text(observableService.name)
Spacer()
Button("Mutate") {
observableService.name += " *"
}
}
.padding()
}
}
@available(iOS 17, *)
#Preview {
let _ = Container.shared.observableService.register { MockObservationService() }
return ObservableView()
} |
Okay, I tested this myself, and you're correct, it works. I have no idea why my previews are now using the default Impl versions and not preview versions of my services, but it doesn't appear to be what I thought it was. |
in a model where i use @observable macro ,
i get 2 errors at the Injected vars
The text was updated successfully, but these errors were encountered: