WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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

            Button("Add") {
                let wizard = Wizard(context: moc)
                wizard.name = "Harry Potter"
            Button("Save") {
                do {
                    try moc.save()
                } 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.


Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, 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.