Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

[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

like image 691
multitudes Avatar asked Aug 26 '20 18:08

multitudes


People also ask

Should I start new project with SwiftUI?

But if you want to ship it fast, if targeting 80% of the iOS ecosystem is enough, then you should definitely go with SwiftUI (once you're comfortable with it). It's a future proof framework and it's faster to iterate on your concept, ideas, and design because of the live preview and declarative API.

Should I use Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

Where is Core Data stored?

The persistent store should be located in the AppData > Library > Application Support directory.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

How to implement core data in SwiftUI with contentview?

But make sure to use SwiftUI as the “Interface” mode and SwiftUI App as the “Life Cycle” mode. Also, make sure that you check the “Use Core Data” box. This will automatically set up the initial Core Data implementation for our app! Make sure you “reset” the ContentView by removing the generated code from it since we won’t need it for our app.

How to create an app with SwiftUI in Xcode 12?

To get started, open Xcode 12 and create a new “App” under “Multiplatform” or “iOS”. You can name your project however you want, for instance, “PizzaRestaurant”. But make sure to use SwiftUI as the “Interface” mode and SwiftUI App as the “Life Cycle” mode. Also, make sure that you check the “Use Core Data” box.

What is the best way to name a SwiftUI project?

You can name your project however you want, for instance, “PizzaRestaurant”. But make sure to use SwiftUI as the “Interface” mode and SwiftUI App as the “Life Cycle” mode. Also, make sure that you check the “Use Core Data” box.

Is the new SwiftUI universal or classic appdelegate?

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?


6 Answers

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)

like image 110
Idan Avatar answered Oct 24 '22 02:10

Idan


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

like image 33
Jacob Avatar answered Oct 24 '22 01:10

Jacob


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://stackoverflow.com/a/66234095/15224199

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.

like image 6
Jakub Jakubowski Avatar answered Oct 24 '22 01:10

Jakub Jakubowski


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).
like image 4
The Mad Bug Avatar answered Oct 24 '22 03:10

The Mad Bug


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

Target Memgership

like image 3
SteffenK Avatar answered Oct 24 '22 03:10

SteffenK


Just clean build folder and restart Xcode works for me.

  1. Clean build folder (SHIFT + COMMAND + K)
  2. Quit Xcode and start again
like image 1
Singh Avatar answered Oct 24 '22 01:10

Singh