BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: BookWorm - How do I "inject a managed object context" into the previews struct?

Forums > 100 Days of SwiftUI

This is what I thought made sense, but the compiler tells me the datacontroller instance cannot be used in COntentView_Previews

struct ContentView_Previews: PreviewProvider {
    @StateObject private var dataController = DataController()
    static var previews: some View {
        ContentView()
            .environment(\.managedObjectContext, dataController.container.viewContext)
    }
}

   

Thanks @Obelix Gotta remember that static thing for Previews...

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.