NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: CoreDataProject question

Forums > 100 Days of SwiftUI


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( ?? "Unknown")

            Button("Add") {
                let wizard = Wizard(context: moc)
       = "Harry Potter"
            Button("Save") {
                do {
                } catch {

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

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


You're missing the entity declaration in your fetchrequest. Insert 'entity: Wizard.entity()' at the start of the fetchrequest.


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?


I assume you have a DataController.swift file set up as well as this ContentView.swift file? If so can you post the code for that too.


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


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.


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.