Created
February 28, 2014 14:04
-
-
Save kristopherjohnson/9271648 to your computer and use it in GitHub Desktop.
TextExpander snippet for a category for a generated Core Data class. Fill in name of class and name of entity.
This file contains 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
// %filltext:name=ClassName:default=ManagedObject%+Operations.h | |
// (for entity %filltext:name=EntityName:default=Entity%) | |
#import "%filltext:name=ClassName:default=ManagedObject%.h" | |
// Category of operations on %filltext:name=ClassName:default=ManagedObject% that are not automatically generated. | |
// | |
// Defining these operations in a category lets us re-generate %filltext:name=ClassName:default=ManagedObject%.h and | |
// %filltext:name=ClassName:default=ManagedObject%.m whenever the schema changes. | |
@interface %filltext:name=ClassName:default=ManagedObject% (Operations) | |
// Return the managed object model entity name ("%filltext:name=EntityName:default=Entity%") for this class | |
+ (NSString *)entityName; | |
// Create a new instance of %filltext:name=ClassName:default=ManagedObject% in the specified context | |
+ (%filltext:name=ClassName:default=ManagedObject% *)insert%filltext:name=EntityName:default=Entity%InContext:(NSManagedObjectContext *)context; | |
// Create a fetch request for %filltext:name=ClassName:default=ManagedObject% objects | |
+ (NSFetchRequest *)fetchRequest; | |
// Create a fetch request for %filltext:name=ClassName:default=ManagedObject% objects with ordered results | |
+ (NSFetchRequest *)orderedFetchRequest; | |
@end | |
// %filltext:name=ClassName:default=ManagedObject%+Operations.m | |
#import "%filltext:name=ClassName:default=ManagedObject%+Operations.h" | |
static NSString * const ENTITY_NAME = @"%filltext:name=EntityName:default=Entity%"; | |
@implementation %filltext:name=ClassName:default=ManagedObject% (Operations) | |
+ (NSString *)entityName { | |
return ENTITY_NAME; | |
} | |
+ (%filltext:name=ClassName:default=ManagedObject% *)insert%filltext:name=EntityName:default=Entity%InContext:(NSManagedObjectContext *)context { | |
return [NSEntityDescription insertNewObjectForEntityForName:ENTITY_NAME | |
inManagedObjectContext:context]; | |
} | |
+ (NSFetchRequest *)fetchRequest { | |
return [NSFetchRequest fetchRequestWithEntityName:ENTITY_NAME]; | |
} | |
+ (NSFetchRequest *)orderedFetchRequest { | |
NSFetchRequest *fetchRequest = [self fetchRequest]; | |
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; | |
fetchRequest.sortDescriptors = @[sortDescriptor]; | |
return request; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, if you enter "KDJCustomer" for the class name and "Customer" for the entity name, this is the result: