Xcode problem if change an entity name

Xcode has problem if I change an entity name in core data. It still tries to use the old name. If I use the editor menu to generate a subclass it generates one with the old name. After I change the entity name from Stack to WestStack I get the following error. What am I doing wrong?


'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'Stack''

Use Xcode's 'Product' menu with the option key pressed, then select 'Clean Build Folder' to force it to forget that cached value/string, etc. - also delete the app off the simulator/device before building again to test - stick w/it until it cooperates.

Now I get two errors. I cleaned the build folder several times but I get the following errors. Is there something else I missed. I appreciate your help.

Thaks

Russ

2015-08-09 11:40:09.843 iFarm[4059:1192939] CoreData: warning: Unable to load class named 'PRODUCT_MODULE_NAME.Stack' for entity 'HayStack'. Class not found, using default NSManagedObject instead.


Could not cast value of type 'NSManagedObject_HayStack_' (0x14de8fc0) to 'iFarm.HayStack' (0x1b6448).

This seems strange as the HayStack class looks like this

import Foundation

import CoreData

class HayStack: NSManagedObject {

@NSManaged var avgwt: NSNumber

@NSManaged var bales: NSNumber

}

Have you changed the class name of the entity as well as the entity name?

Yes both are named HayStack.

Here is the class.


import Foundation

import CoreData

class HayStack: NSManagedObject {

@NSManaged var avgwt: NSNumber

@NSManaged var bales: NSNumber

}

On the one hand, the error message says, ...' class named 'PRODUCT_MODULE_NAME.Stack' for entity 'HayStack'. And on the other hand, you say, 'Yes both are named HayStack.'


Is there some inconsistency with the names 'Stack' and 'HayStack'?

Seems you may not have changed all uses of 'Stack'. Search the project on: Stack (...be sure to exclude haystack - maybe search on .Stack)

I don't mean the Swift source file of the NSManagedObject class. Check the Class attribute of the Data Model inspector.

You were right. I changed the class in the Class attribute in the Data Model inspector to HayStack. Now I get


2015-08-10 15:39:20.764 iFarm[4406:1319927] CoreData: warning: Unable to load class named 'HayStack' for entity 'HayStack'. Class not found, using default NSManagedObject instead.

and

Could not cast value of type 'NSManagedObject_HayStack_' (0x1669b170) to 'iFarm.HayStack' (0x10b448).

Have you entered the Module name, under the Class in the Data Model inspector?

I changed the name of the class in the data model inspector to PRODUCT_MODULE_NAME.HayStack. Is this what I was supposed todo?

I get the following warning and error.

2015-08-11 08:46:54.434 iFarm[4535:1392314] CoreData: warning: Unable to load class named 'PRODUCT_MODULE_NAME.HayStack' for entity 'HayStack'. Class not found, using default NSManagedObject instead.

Could not cast value of type 'NSManagedObject_HayStack_' (0x15d5dc40) to 'iFarm.HayStack' (0xed448).

I have no idea how modules got involved in this project. I started with two projects, both working fine, and copyied and pasted code to combine the two into one project. I had to change the code to use the same entity names and I expected everything to work. Wrong again. There must be something wrong with how I combined the code form the two projects.

The line that causes the problem is

let inventory =

fetchedResultsController.objectAtIndexPath(indexPath) as! HayStack


As you can tell I am lost here. I do appreciate you taking the time to help me!

Accepted Answer

I have no idea how modules got involved in this project.

All Swift classes are implicitly namespaced with module name. In a usual project settings, the module name is your product name, and it's the same as your project name.

So, if you defined a class named `HayStack` in a project named `iFarm`, its actual (meaning visible from Objective-C runtime) class name is `iFarm.HayStack`.


And giving an @objc name is another way to solve the issues around module names.

@objc(HayStack)
class HayStack: NSManagedObject {

(@objc names are not prefixed with module name.)


And set the `Class` field in the Data Model inspector to the @objc name -- HayStack. Do not put a module name or `.`.

If your Data Model inspector shows separate `Module` field under the `Class` field, make it empty.

Thank you. Thank You!! It works. Some how when I copied code from one project to another the module name must have been copied as well, resulting in references to two different product names.

Xcode problem if change an entity name
 
 
Q