Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MagicalRecord createEntity error in Swift

I have a crash in my Swift project with MagicalRecord - CoreData library : https://github.com/magicalpanda/MagicalRecord

First, I setup MagicalRecord OK in AppDelegate.swift:

MagicalRecord.setupCoreDataStack() //-> This is OK. Don't crash

But, when I want to create a "Contact" entity instance I got a Crash.

My code is the next:

var context = NSManagedObjectContext.MR_contextForCurrentThread() // -> This is OK. Don't crash
var contact = Contact.MR_createInContext(context) // -> CRASH

All compile OK but when the App is running I get the next crash:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'MyProject.Contact' in this model.' *** First throw call stack: 
(0x27861f77 0x34870c77 0x27590c73 0x9b93d 0x74f28 0x75438 0x2aea6e33 0x2b09acef 0x2b09d19d 0x2b0a79f9 0x2b09ba5b 0x2e106141 0x2782881d 0x27827ae1 0x2782627f 0x27773da1 0x27773bb3 0x2aea0ae7 0x2ae9bc41 0x75ab0 0x75aec 0x34e10aaf) 
libc++abi.dylib: terminating with uncaught exception of type NSException

MyProject key is the $(PRODUCT_NAME), how I can fix this?

Kind regards

like image 999
Paolpa Avatar asked Dec 05 '22 05:12

Paolpa


1 Answers

There is a simple solution to this issue. Just add @objc(ClassName) before the swift NSManagedObject subclass. This allows the Objc side know the right name for the class. Otherwise it will include the module name which is bad in this case because it won't match what you have in your Core Data model. Here's and example from one of my swift projects...

@objc(Plant) public class Plant: NSManagedObject

like image 115
Jon Chmura Avatar answered Dec 23 '22 06:12

Jon Chmura