protocol ExampleStore: ObservableObject {
var text: String { get }
}
class ConcreteExampleStore: ExampleStore {
@Published var text: String = ""
}
struct ExampleView: View {
@ObservedObject private var store: any ExampleStore
init(store: any ExampleStore) {
self.store = store
}
var body: some View {
Text(store.text)
}
}
Im exploring the new features of Swift 5.7 and was trying to make a Store protocol which I would use as a type inside my View. I know that this would not be possible in previous versions of Swift since the ObservableObject protocol which ExampleStore conforms to has an associatedType and therefore the workaround was to define a generic parameter in the View which conforms to ExampleStore protocol, but since 5.7 those protocols should be usable as existential types?
I'm getting the following error: Type 'any ExampleStore' cannot conform to 'ObservableObject'. It is strange that the message appears on the line where the store is declared as a property of the View, can anyone make sense of this error message? Thanks in advance :)