Xcode 11 doesn't recognize Core data Entity - swift

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

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.

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.

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:
In the Project navigator, select your .xcdatamodelId file
In the Configurations section, select the appropriate configuration
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)

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.

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.

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.

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.

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

Related

Codegen for CoreData entity not working in Xcode 11

So I am pretty new to CoreData in Xcode and I have been looking for tutorials to see how to get more familiar with it and start working with it. A tutorial I have been watching, mentions that we need to Generate code for an entity that I created. I select Editor -> Create NSManagedObject Subclass to generate the code for the entity. But the files generated come full of errors and warnings that I haven't caused. I will attach screenshots for some more details.
That's the entity that I am creating in CoreData
That's the first generated file for the entity
That's the second file generated for the entity
The below are the errors and warnings I am getting:
File 'Person+CoreDataClass.swift' is part of module 'CoreData'; ignoring import
File 'Person+CoreDataProperties.swift' is part of module 'CoreData'; ignoring import
Only classes that inherit from NSObject can be declared #objc
Cannot find type 'NSManagedObject' in scope
Cannot find type 'NSFetchRequest' in scope
Note that I have enabled CoreData when I first created the project and I haven't made any changes to any other files in the project.
I would appreciate any kind of help since I haven't got any experience yet with CoreData. Thank you!
Try creating a new project with a different name (not CoreData)

Core Data Entities multiple build configurations

I have 3 build configurations, Debug, Release and Development. For Development I have created a new Scheme with a new bundleID, Product name etc so I can see which one is DEV and DEBUG on my phone.
I am using Core Data and for the DEBUG with the original bundleID everything works fine, but when I switch to use the Development one, I get an error when I try to create a new entity with name:
NSEntityDescription.insertNewObject(forEntityName: "MyEntity", into: managedContext)
The error I get:
Could not cast value of type 'NSManagedObject_MyEntity_' (0x600002d62b20) to 'AppName_DEV.MyEntity' (0x102bf9368).
It seems like it is trying to get the MyEntity class from my "AppName DEV" scheme not the "AppName". I can't find any Build Settings where I could point it to use the same model?
Any ideas on how to use core data with multiple build configurations?
Ok, I found the problem. Since I was trying to revive a very old app originally written in swift 2, it occurred to me that I might double check how was the data model created. After hours of tinkering with the core data set up process, I changed 2 things that finally get it to work.
1.) In the .xcdatamodeld file, on the right side in the Data Model Inspector, make sure that you have the Module field EMPTY I had my AppName Inside, which was the same as the original target name.
2.) I added #objc(EntityName) to the NSManagedObject subclass I had.
Of course I cleaned the project, deleted the app from the simulator and installed it again.
If you have an issue like that, hope that whis will help you, good luck!

Preventing a CoreData crash for upgrading users

I built an app about a year and a half ago that I'm coming back to. It was the project I cut my Swift teeth on and obviously a lot has changed since then, both in the language and my Swift abilities.
Yesterday for the first time, I updated my single CoreData model to add an optional string attribute. I did the file generation bit and made sure in the inspector column the new data model is appropriately selected.
On my Simulator and testing devices, I need to delete the old version of the app to install the new version or I get a crash. I assume that's just part of the development environment process. How can I ensure that upgrading users won't have to delete and reinstall when they do a simple update from the App Store? I assume that Xcode/CoreData/Apple have this handled with some internal scripts or processes that are invisible to the user, "it just works." But I wanted to post this here to understand if there's anything additional I need to do to ensure a smooth transition from v1 to v1.1 for the user.
All I did was an an optional string column, as I mentioned. I assume that all existing user data will be migrated over to the new schema with the new field being nil.
Any thoughts here would be very welcomed and appreciated. Thanks!
If your app is crashing in the Simulator when upgrading, your users will have crashes too.
To avoid this, you need to make sure you follow these steps:
Make sure you do NOT change the original version of your data model in any way.
In Xcode, select your xcdatamodel file, then from the menu choose Editor > Add Model Version...
Xcode will suggest a new version name, based on the current model. Make a mental note of the new version name, then click Finish.
Select the xcdatamodel file again, go to File inspector and under Model Version, select the new version name to make this your current version.
In Project Navigator, select the new version of the xcdatamodel. Add your attribute.
It's important to follow these steps in this order. If you add your attribute before creating the new model or making it your current version, you will have crashes.
EDIT: This will only work if you enable lightweight migrations. This is a code snippet of how to do this:
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
do {
//coordinator is an NSPersistentStoreCoordinator
try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options
} catch var error as NSError {
// handle error however you want here...
abort()
}
In addition to #Mike Taverne's answer, I think it a better if I post some screenshots to illustrate the option
for Xcode 12.3
Choose from the main menu Editor -> Add Model Version
To add mark the New Model as the current model with a green checkmark
Follow the below image

Why I get warnings using “imageTitle” as an attribute name for a CoreData entity?

I get warnings in Xode 4 using “imageTitle” as an attribute name for a Core Data entity? In Xcode 3 there was no warning...any idea why?
The exact warning is: Naming Conflict: imageTitle conflicts with a method already on NSObject or NSManagedObject
Very annoying is that I get this warning for every single past Model Version...
Any idea how I get rid of these warnings for past Model Versions?
EDIT: You easily can reproduce this warning, if you create a new project using the "Navigation-based Application" with CoreData support and adding just the attribute "imageTitle"...
I can't answer why imageTitle is specifically targeted. My only guess is that it conflicts with some internal method or core data attribute.
In the past, if you had a core data attribute that used one of the reserved words, Xcode would let you build and your project would display very strange behavior. The most common example is "description". Cocoa assumes that description returns an NSString that describes your object, usually for logging. If you had a dynamically generated core data attribute named description, then bad things would happen. Especially if it was data or transformable.
Current versions of Xcode give the same error dialog if you try to name an attribute description.
While this is irritating, I highly recommend changing your attribute name to something else, such as pictureTitle or imageTitleString. Even though "imageTitle" doesn't get many hits in the documentation, it likely conflicts with something internal.
As a last resort, file a bug with Apple. It may actually just be a bug in Xcode.

Cant find Manage Object Class in xCode for iPhone

I am trying to generate classes for my .xcdatamodel entities, but am not given the Manage Object Class option when I click on an entity and select File>New File. I've looked under both the iPhone and Mac OS X catagories, but it is not listed anywhere.
Here are the steps I am taking,
I create a new project being sure to check the "Use Core Data" checkbox. Once the project is created, I open .xcdatamodel, create an entity, and add a few attributes. Next, I click on the entity and choose File>New File, but all I see are only Objective-C, Objective-c test case class, and UIVewController subclass.
Am I missing a step?
Thanks in advance
Take a look at the Core Data Tutorial for iPhone OS and look at this section: Custom Managed Object Subclass. If you still aren't seeing the Core Data related items for the iPhone OS check the version of the SDK you are targeting. I believe Core Data has only been available since SDK 3.
Had the same problem. Restarting x-code helped me.