BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: CoreDataProject question

Forums > 100 Days of SwiftUI

Hello,

On CoreDataProject, when I get to the part with code:

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc

    @FetchRequest(sortDescriptors: []) var wizards: FetchedResults<Wizard>

    var body: some View {
        VStack {
            List(wizards, id: \.self) { wizard in
                Text(wizard.name ?? "Unknown")
            }

            Button("Add") {
                let wizard = Wizard(context: moc)
                wizard.name = "Harry Potter"
            }
            Button("Save") {
                do {
                    try moc.save()
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I keep getting the following errors: "Cannot find type "Wizard" in scope" "Unable to infer type of a closure parameter for 'wizard' in the current context"

Because of the errors, I'm not able to run the code.

I've started this project from the beginning 3 times now to try to find errors from my end but I cannot. Will appreciate it if someone will point me in the correct direction so that I can move on with the project.

Thank you

2      

I tried using :

@FetchRequest(entity: Wizard.entity(), sortDescriptors: []) var wizards: FetchedResults<Wizard>

but still have the same error. I don't see it in Paul's tutorial anywhere. Any other ideas?

2      

In your CoreData Model under the Wizard entity. What did you select for CodeGen? It should be Class Definition for this example.

2      

I solved the issue. The problem was that I dragged & dropped the DataController file directly from the Bookworm app into the CoreDataProject app.
Even though I changed the NSPersistentContainer name to "CoreDataProject", for some reason it created a problem.
I finally deleted the DataController and made a brand new one and the problem disappeared.

Thank you all for the ideas.

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.