ObjectiveRecord icon indicating copy to clipboard operation
ObjectiveRecord copied to clipboard

.create() call broken in Swift as of Swift 1.2 (xcode 6.3-beta)

Open Tylerc230 opened this issue 10 years ago • 6 comments
trafficstars

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.

Tylerc230 avatar Feb 12 '15 18:02 Tylerc230

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;
}

bartjacobs avatar Feb 19 '15 14:02 bartjacobs

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 .

Tylerc230 avatar Feb 19 '15 16:02 Tylerc230

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).

DenTelezhkin avatar Feb 19 '15 17:02 DenTelezhkin

This will fix your woes

@objc(MyManagedClass)
class MyManagedClass: NSManagedObject {

}

NSStringFromClass(MyManagedClass)

image

hdost avatar Mar 10 '15 19:03 hdost

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

  1. Removed obsolete CoreData code from AppDelegate (earlier I have added it manually).
  2. Created new xcdatamodeld named like my project, let's say: MyProject.xcdatamodeld.
  3. Added new entity Sample with few fields and generated a class or it. In entity the configuration I tried both MyProject.Sample and Sample as a class.
  4. Tried with with overriding entityName and @objc annotation.

I managed to get such error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'Sample'

tomaszwojcik avatar Apr 21 '15 13:04 tomaszwojcik

Just popped a swift version. Cheers! https://github.com/arkverse/SwiftRecord

reaperdtme avatar May 08 '15 15:05 reaperdtme