Search code examples
viewswiftuiobservedobject

Passing a class instance into a PreviewProvider in SwiftUI?


so in my ContentView, I'm creating a view with:

ViewName(variable: ObservedObjectInstance)

where variable is the name of an initialization parameter required for the view; it takes in something of the same type as the ObservedObjectInstance. The ObservedObjectInstance is an @ObservedObject variable that was declared as a struct-level variable. In the ViewName class, I'd like to be able to access ObservedObjectInstance but I'm not sure how. I tried just declaring the following as a struct-level variable:

var variable: ObservedObjectClassType

and then in the PreviewProvider, I tried doing the following:

ViewName(variable: variable)

but it didn't work. I'm pretty sure the declaration of 'variable' in the ViewName class and the PreviewProvider is what's wrong, but I'm not sure how to fix it. Any help would be greatly appreciated.


Solution

  • In PreviewProvider use the following

    ViewName(variable: ObservedObjectClassType())