UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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


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?


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.


TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

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.