Created
April 16, 2015 21:34
-
-
Save nevyn/d22c4684370fa07078dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation NSManagedObject (TCCreation) | |
+ (instancetype)tc_insertIntoContext:(NSManagedObjectContext*)ctx | |
{ | |
for(NSEntityDescription *desc in [ctx.persistentStoreCoordinator.managedObjectModel entities]) { | |
if([desc.managedObjectClassName isEqual:NSStringFromClass(self)]) { | |
return [[self alloc] initWithEntity:desc insertIntoManagedObjectContext:ctx]; | |
} | |
} | |
NSAssert(NO, @"This class does not exist in the managed object model for %@", ctx); | |
return nil; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSManagedObject { | |
class func tc_insertIntoContext(context: NSManagedObjectContext) -> Self? | |
{ | |
let thisClassName : String = NSStringFromClass(self as! AnyClass) | |
for desc in context.persistentStoreCoordinator!.managedObjectModel.entities as! [NSEntityDescription] { | |
if desc.managedObjectClassName == thisClassName { | |
return self(entity:desc, insertIntoManagedObjectContext:context) | |
} | |
} | |
assert(false, "This class does not exist in the managed object model for \(context)") | |
return nil; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @nevyn.
Oops, I spoke too soon on the meetup page.
So, this here will compile:
There are still some major drawbacks though:
O(n)
operation 😢AnyObject?