Xcode 11 doesn't recognize Core data Entity
Asked Answered
R

9

15

I just declared an entity called "Users" array:

var UsersArray = [Users]()

I got this error:

use of unresolved identifiers "Users"

hint : I did import CoreData and created the entity

Rudiment answered 31/10, 2019 at 22:21 Comment(5)
Umm, where is Users defined? To have an array of an object, that object must be defined somewhere. You need to create a class or struct of that class to create an array of it.Uniliteral
its in a viewController , i thought CoreData creates it's data models classes by itself that's why i did not create the entity "Users" classRudiment
Ahh, i see. Can we see your entity model?Uniliteral
Make sure the the Codegen property for the entity is set to "Class Definition" in your model otherwise you need to generate the classes yourself under "Editor->Create NSManagedObject Subclass...". After editing or changing the model it might be necessary to clean and rebuild the project so try that if you haven't already.Woods
@JoakimDanielson it's supposed to be generated automatically when i add the entity no ? i am using xcode 11Rudiment
K
35

Please try this solutions !!

Solution 1 :- Product->Build for testing solved this issue for me(it solves most unresolved identifier bugs for some strange reason)

Solution 2 :- changing import Foundation to import UIKit. It seems like UIApplication isn't included in Foundation framework.

Solution 3 :- Highlight the Data Model, go to Editor -> Create NSManagedObject Subclass...

Solution 4 :- Create the NSManagedObject files and then delete them and everything is fixed.

Hope this helps thank you.

Kicksorter answered 6/11, 2019 at 16:41 Comment(5)
Had the same exact issue after updating to Xcode 11. Building/running it fixed it.Dune
Great Happy Coding :-)Kicksorter
solution 3 works. actually you can create the NSManagedObject files and then delete them and everything is fixedSpate
I updated the answer and added your suggestion as solution 4. So It will helpful for all. Thanks!Kicksorter
Awesome, thanks! It was Solution 4 which solved my problem.Maciemaciel
T
26

If your entity is actually created, just close and re-open XCode. This works for me everytime. The other solutions, such as clean build, build etc, didn't work for me.

Trevino answered 20/6, 2020 at 15:18 Comment(2)
Thanks. This should be the first attempt to solve this issue!Monogamous
I dont know why Apple couldn't fix this. I tried to clean, Delete Derived data and none worked. I created models manually too (Pity on me) before trying this.. Never the late, thanks for the tipUnstriped
U
4

Just ran into this issue after renaming an entity and figured I should post here how I resolved it.

Typically after making changes in the data model and getting compilation errors, I just do Product > Clean Build Folder, then Product > Build and that should take care of the errors.

However, the steps above didn't help this time. After scratching my head for while, I found the culprit with following steps:

  1. In the Project navigator, select your .xcdatamodelId file
  2. In the Configurations section, select the appropriate configuration
  3. Look for the entity in question and make sure the class name is correct (in my case, the entity and the class were not the same)
Usk answered 26/3, 2020 at 4:58 Comment(2)
Saw this on a new project. It had created a 'Default' configuration. After selecting the .xcdatamodeld file, saw under Identity and Type that Type was 'Default - Core Data Model'. Switched to 'Core Data Model'. Compiled and error was gone. Could then switch back to 'Default - Core Data Model' and error did not come back. Suspect exiting project (or maybe Xcode) and coming back might have similar effect.Dibbuk
Somewhat similarly, I changed Type to Application Bundle. Panicked. CoreData had diasppeared, but I did Files / Add Files to... and re-added the .xcdatamodeld file, and was able to compile successfully. The Type had gotten set back to "Default - Core Data Model." This was after trying everything else I have been able to find here and elsewhere. grumble. All I want to do is find a way to write a custom initializer for an entity. My entire CoreData data mysteriously -vanished- earlier, too. Grumble...Heirship
A
2

Other possible solutions:

(A) Re-launch Xcode

This is the solution that worked for me. (See this answer)

(B) Check if you did not forget to finalize your entity description

Typically: forgot to assign a type to one of the attributes (still "undefined").

(C) Clear derived data

Reason: The generated core data managed objects subclasses's source code files are in the project’s derived data location. Note: you might have to re-link your swift packages is using SPM.

Asterisk answered 28/5, 2020 at 4:22 Comment(0)
W
0

I also had to deal with this issue even working with the latest Xcode12.3 on a SwiftUI project where I didn't checked the CoreData option at project creation.

After adding a class to instantiate the NSPersistentContainer, loading the stores, creating model.xcdatamodel file with an entity and a text field plus a button on a view, then it shows an error saying: "cannot find Item in scope".

It's a bit difficult to write code when you expect the entity to help out with autocomplete. First, I thought I forgot to initialize my class or missed a bracket, but it wasn't that either.

I tried cleaning up, rebuilding and running the project several times with build failed as result. I had to close this particular project then all errors got dismissed. No needed to restart Xcode.

Wirer answered 5/1, 2021 at 3:9 Comment(0)
M
0

I faced the same issue. Doesn't included Coredata on the beginning of the project. Later added necessary files and core data stack. But when trying to fetch the data from the Entity name and the Xcode throwing the same error.

Later, I closed the project and again opened on Xcode and it started to work.

Mizzenmast answered 24/8, 2021 at 3:8 Comment(0)
H
0

I had this issue with an .xcdatamodel that I had created in another project and moved it into this one.

I solved it by selecting the .xcdatamodel file, turned of the "File Inspector" and then under "Target Membership", I made sure that all the targets were selected.

Hereafter answered 30/5, 2022 at 16:19 Comment(0)
H
0

for me the entity name and class name was different, and I was refering to entity name for Data Type

enter image description here

Howdy answered 29/7, 2022 at 20:52 Comment(0)
D
0
  1. Click on your data Model ex : DataModel.xcdatamodeld
  2. Click Editor > Create NSManagedObject Subclass It will create class Item by extension CoredataClass and one other. Delete all them (file that just generate)
  3. Now try to Build project again
Deibel answered 26/7, 2023 at 3:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.