Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

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

Open
Tylerc230 opened this issue Feb 12, 2015 · 6 comments
Open

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

Tylerc230 opened this issue Feb 12, 2015 · 6 comments

Comments

@Tylerc230
Copy link

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.

@bartjacobs
Copy link

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

@Tylerc230
Copy link
Author

My work around was to override entityName in a subclass.

On Thursday, February 19, 2015, Bart Jacobs notifications@github.com
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
#115 (comment)
.

@DenTelezhkin
Copy link

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

@hdost
Copy link

hdost commented Mar 10, 2015

This will fix your woes

@objc(MyManagedClass)
class MyManagedClass: NSManagedObject {

}

NSStringFromClass(MyManagedClass)

image

@tomaszwojcik
Copy link

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'

@reaperdtme
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants