SwiftUI 2.0 CoreData issues with new project - 'Cannot find type 'Item' in scope'
Asked Answered
T

11

55

[EDIT] I was hoping that Apple would have fixed what is obviously a bug. The solution is to clear the cache, close and reopen Xcode..

I am on Xcode Beta and starting a new project and without writing a single line of code I already get an error. I could not find anything online. Perhaps is this too new?

In the new version of Xcode I selected new Project,

selecting new project

then ticked the box with Core Data

ticking the box Core Data

If I do not tick the box I would get the usual Xcode SwiftUI template with "hello world", but ticking the Core Data box I get a ton more template code from Apple and without touching anything and without changing a single line of code, I get an error message compiling it..

Error

The error is: "Cannot find Item in scope"

Quite frustrating, especially because all tutorials I have start with the classic 'AppDelegate' file configuration.. while the new SwiftUI is 'universal'!

I checked the file .xcdatamodeld and it looks fine, and has the 'Item' entity. Why it doesn't compile?

So I am now at a loss, is there a solution or this is a bug and need to wait that Apple releases a fix. If so I do not need to start with a new project until then!

PS Today I cleaned the cache with CMD-ALT-SHIFT-K, closed Xcode, deleted the app from the simulator, reopened, rebuilt and it did compile.. but nothing in the simulator! We are making progress! Still I did not change a line of code. Everything is the Apple template yet!

enter image description here

Trapan answered 26/8, 2020 at 18:35 Comment(5)
It's Xcode caching... restart, reopen, rebuild.Buddy
I absolutely hate CoreData. Every time I give it a try something like this happens. Have tried everything including the DerivedData delete method. Nothing works. I can't even get Apple's bare bones template that is generated for you to run. Come on Apple!Gavin
Yes I had the exact same problem today with Xcode 12.3 I just closed Xcode completely and then restarted it and everything built fine. Even with the error I was able to do a build though could not run the project on the simulator. So if you ever get an error that you cannot find the Entity in scope try restarting Xcode.Eudo
Still the same annoying bug in Xcode Version 12.4Vitalis
I followed the above advice and used <kbd>Cmd</kbd>+<kbd>B</kbd> and it worked. It asked me to Revoke and then authorize my current computer.Threnode
P
68

The normal Xcode clearing works for me:

  1. Clean build folder (SHIFT + COMMAND + K)

  2. Quit Xcode completely

  3. Delete the project's contents in, DerivedData/{Project Name}_some_hash

    The default location is ~/Library/Developer/Xcode/DerivedData, but if it's nowhere to be found, check the Derived Data property under, Xcode → Preferences → Locations

  4. Try again (run Xcode & build)

Peppercorn answered 27/8, 2020 at 12:6 Comment(6)
I was hoping Apple would have fixed it by now... 😂 But yeah still need to restart for every new project!Trapan
Step 3 seems not to be necessary !Vitalis
Once you clear the erroneous compiler errors, you'll still be left with a sample project that doesn't do anything. See Jakub Jakubowski's answer below (including his linked answer) to actually make the App useable.Cheapskate
Clean build folder (SHIFT + COMMAND + K) then pod update solve my problemCrumpton
I can't believe that this is still an issue in June 2022, but here we are.Grantley
man. I hate it if the solution is to clean itJudsonjudus
P
12

For a brand new project, press Command+B to build and it will be fine.

Protector answered 23/10, 2020 at 10:32 Comment(1)
When using Core Data this is definitely not working with the current Xcode's (12.5.1 and 13b6)Chiliasm
A
6

Firstly it's not an issue with your app but an issue with a preview. Your app works properly on the simulator. The white screen is because you need to wrap your list with NavigationView() to see add and edit button. See this answer: https://mcmap.net/q/339274/-select-and-delete-core-data-entities-in-swiftui-list-view-on-macos

After that, you will see add and edit button on the simulator. But you have to fix the preview too. It doesn't work because you have an empty entity and you need to mock it. Go to Persistance.swift and you should add similar for loop as mine to create mocked Items in preview varable:

static var preview: PersistenceController = {
    let result = PersistenceController(inMemory: true)
    let viewContext = result.container.viewContext
    for _ in 0..<10 {
        let newItem = Item(context: viewContext)
        newItem.timestamp = Date()
    }
    do {
        try viewContext.save()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        let nsError = error as NSError
        fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
    }
    return result
}()

At the end make sure that your preview use those mocked values:

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
    ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}

After that, it should work perfectly fine, hope it helps. I don't know why Apple provides a template that doesn't work properly at the beginning.

Adventitia answered 17/2, 2021 at 12:14 Comment(0)
E
4

I didn't find any of the others answers worked for me, but what did was:

  • Opening (ProjectName).xcdatamodel
  • Adding an attribute to the Item entity, shouldn't relly matter what, I just add "foo" of type "String"
  • Cmd-B to build (You are then free to delete the new attribute).
Emeritaemeritus answered 15/11, 2020 at 6:3 Comment(0)
H
4

If you still have problems, you may forgot to add your .xcdatamodel file to the Test Target.

Target Memgership

Hindi answered 19/2, 2021 at 19:2 Comment(1)
it has worked for me. I had to recreate the project with the "include Tests" option activated. For references : XCode 14.3.1 , the ML model was SqueezeNetFP16Odalisque
D
1

In my case the problem was: Core data was extending model, but i newer created struct model for it to extend... silly mistake... When you get cant find in scope error always read the line above (with warning) thats most likely the real problem...

Danitadaniyal answered 25/3, 2022 at 22:53 Comment(1)
Similar to me, I was just starting to make changes to the data model and I got the error "Cannot find Item in scope". I reverted back to a backup of the model before I made changes and the error went away. thanks goodness for backups. Now to proceed slowly.Sivan
E
1

Just clean build folder and restart Xcode works for me.

  1. Clean build folder (SHIFT + COMMAND + K)
  2. Quit Xcode and start again
Encipher answered 28/4, 2022 at 9:41 Comment(1)
I have done, but not working!Conaway
P
0

Project Clean and Build works for me.

Parliamentary answered 24/11, 2021 at 10:53 Comment(0)
B
0

Just throwing this in here because I've been struggling with this issue for ages and none of the previous suggestions had worked for my issue. With mine, I was trying to move an old CoreData set over to a much newer SwiftUI build (remaking the app from scratch) and I can't promise if you are in the same situation this will solve all your issues.

I had to highlight my CoreData entity (I only had one) and then under its "Data Model Inspector" "Class" section change "Codegen" to "Class Definition". I had been set to "Manual / None". I only mention this in case it helps someone in a similar situation and is probably not the issue most are having.

Butyrin answered 7/10, 2022 at 23:43 Comment(0)
D
0

Generating the models is what worked for me.

Select .xcdatamodeld file in folder hierarchy
Editor -> Create NSManagedObject subclass enter image description here

Donelu answered 2/5, 2023 at 9:33 Comment(0)
D
0

I'm running Xcode v14.3.1(14E300c)

xcode version v14.3.1(14E300c)

I was having the problem where it stated that it could not find the type in scope even though I had defined it previously in Core Data.

Closed & Restarted Xcode

I closed Xcode and started it again and when Xcode started again it recognized the type.

Duiker answered 9/7, 2023 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.