7

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!

Besi
  • 22,579
  • 24
  • 131
  • 223
Eddz
  • 71
  • 3
  • Darn, I wasted a whole evening trying to figure out why I was getting this error. One way I attempted to fix it was to replace the `representedClassName` with `projectName.className` instead of simply `className`. That actually fixed this specific error, but later started throwing exceptions in RestKit mapping after data was returned by my Restful API. – Shiprack Nov 17 '14 at 18:41
  • I did file a RestKit issue, because generally the NSManagedObject subclasses are generated and should not be changed manually... https://github.com/RestKit/RestKit/issues/2181 – Besi Mar 07 '15 at 13:26

0 Answers0