ObjectiveRecord
ObjectiveRecord copied to clipboard
.create() call broken in Swift as of Swift 1.2 (xcode 6.3-beta)
The create() call fails because, in createInContext: the call to [self entityName] returns 'ModuleName.Classname" instead of just "Classname" as it did in Obj-c. The format of the return type of NSStringFromClass changed in the latest Swift release.
This could be solved by inspecting the result of NSStringFromClass, breaking the string up in components separated by ., and returning the last component.
+ (NSString *)entityName {
NSString *entityName = NSStringFromClass(self);
if ([entityName containsString:@"."]) {
NSArray *components = [entityName componentsSeparatedByString:@"."];
if (components.count > 1) entityName = components.lastObject;
}
return entityName;
}
My work around was to override entityName in a subclass.
On Thursday, February 19, 2015, Bart Jacobs [email protected] wrote:
This could be solved by inspecting the result of NSStringFromClass, breaking the string up in components separated by ., and returning the last component.
(NSString *)entityName { NSString *entityName = NSStringFromClass(self);
if ([entityName containsString:@"."]) { NSArray *components = [entityName componentsSeparatedByString:@"."]; if (components.count > 1) entityName = components.lastObject; }
return entityName; }
— Reply to this email directly or view it on GitHub https://github.com/supermarin/ObjectiveRecord/issues/115#issuecomment-75058296 .
I've been struggling with class names in my frameworks as well, here's the solution i came up with:
https://github.com/DenHeadless/DTModelStorage/blob/master/DTModelStorage/Utilities/DTRuntimeHelper.m#L13-L22
This is basically NSStringFromClass, but language-independent, which returns class name in objective-c, and second part of class name in Swift(first is module name).
This will fix your woes
@objc(MyManagedClass)
class MyManagedClass: NSManagedObject {
}
NSStringFromClass(MyManagedClass)

I use Swift 1.2 and XCode 6.3 and I'm still getting the Cannot create an NSPersistentStoreCoordinator with a nil model error.
Any ideas where I failed? Here's what fails and my steps:
var sample = Sample.create() as! Sample
- Removed obsolete CoreData code from AppDelegate (earlier I have added it manually).
- Created new
xcdatamodeldnamed like my project, let's say:MyProject.xcdatamodeld. - Added new entity
Samplewith few fields and generated a class or it. In entity the configuration I tried bothMyProject.SampleandSampleas a class. - Tried with with overriding
entityNameand@objcannotation.
I managed to get such error:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Sample'
Just popped a swift version. Cheers! https://github.com/arkverse/SwiftRecord