Skip to content

Instantly share code, notes, and snippets.

@Appletone
Created January 7, 2016 14:04
Show Gist options
  • Save Appletone/bf51e30717e30a816065 to your computer and use it in GitHub Desktop.
Save Appletone/bf51e30717e30a816065 to your computer and use it in GitHub Desktop.
Create a new NSManagedObject with init method (Xcode 7.2 Swift 2.1 ) from: http://stackoverflow.com/a/33583941
extension NSManagedObject {
// Returns the unqualified class name, i.e. the last component.
// Can be overridden in a subclass.
class func entityName() -> String {
return String(self)
}
convenience init(context: NSManagedObjectContext) {
let eName = self.dynamicType.entityName()
let entity = NSEntityDescription.entityForName(eName, inManagedObjectContext: context)!
self.init(entity: entity, insertIntoManagedObjectContext: context)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment