How can I create instances of managed object subclasses in a NSManagedObject Swift extension?

(Updated for Swift 3/4 now. Solutions for earlier Swift versions can be found in the edit history.) You can use unsafeDowncast to cast the return value of NSEntityDescription.insertNewObject() to Self (which is the type on which the method is actually called): extension NSManagedObject { class func create(in context: NSManagedObjectContext) -> Self { let classname = … Read more

Delete/Reset all entries in Core Data?

You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method. NSPersistentStore *store = …; NSError *error; NSURL *storeURL = store.URL; NSPersistentStoreCoordinator *storeCoordinator = …; [storeCoordinator removePersistentStore:store error:&error]; [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; Then, just add the persistent store back to ensure it is recreated properly. The programmatic way for iterating through each entity is both … Read more