CoderTn
CoderTn

Reputation: 997

Xcode 11 doesn't recognize Core data Entity

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

Upvotes: 15

Views: 12986

Answers (9)

Bagus Rizky M
Bagus Rizky M

Reputation: 11

  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

Upvotes: 0

Mrudul Addipalli
Mrudul Addipalli

Reputation: 624

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

enter image description here

Upvotes: 0

Varcos
Varcos

Reputation: 11

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.

Upvotes: 0

Kuringan
Kuringan

Reputation: 147

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.

Upvotes: 2

Kavya Joshi
Kavya Joshi

Reputation: 41

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.

Upvotes: 0

Yogesh Patel
Yogesh Patel

Reputation: 2017

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.

Upvotes: 35

Serg
Serg

Reputation: 188

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.

Upvotes: 0

LondonGuy
LondonGuy

Reputation: 11098

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.

Upvotes: 26

xcoder
xcoder

Reputation: 1411

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)

Upvotes: 4

Related Questions