Swift RestKit CoreData - RKEntityMapping nil class for class name

942 Views Asked by At

Using RestKit and CoreData with Swift

RestKit offers functionality to map JSON directly into NSManagedObject instances. You use the following code line to init a RKEntityMapping object:

var classMapping :RKEntityMapping = RKEntityMapping(forEntityForName:"className",inManagedObjectStore:managedObjectStore)

This causes the following error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot initialize an entity mapping for an entity with a nil managed object class: Got nil class for managed object class name 'className'. Maybe you forgot to add the class files to your target?'

The error occurs because of this line in RestKit API:

Class objectClass = NSClassFromString([entity managedObjectClassName]);

Solution:

If your NSManagedObject class is written in Swift, you have to add the following code before your class declaration:

@objc(className)

class className {
...
}

Hope this helps!

0

There are 0 best solutions below