Skip to content

Instantly share code, notes, and snippets.

@3lvis
Created April 22, 2015 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3lvis/d84020ba97bb7f16c0a5 to your computer and use it in GitHub Desktop.
Save 3lvis/d84020ba97bb7f16c0a5 to your computer and use it in GitHub Desktop.
Generating a Core Data Model from code
- (void)generateModel {
NSManagedObjectModel *model = [NSManagedObjectModel new];
NSEntityDescription *runEntity = [NSEntityDescription new];
runEntity.name = @"Run";
runEntity.managedObjectClassName = @"Run";
model.entities = @[runEntity];
NSMutableArray *runProperties = [NSMutableArray new];
NSAttributeDescription *dateAttribute = [NSAttributeDescription new];
dateAttribute.name = @"date";
dateAttribute.attributeType = NSDateAttributeType;
dateAttribute.optional = NO;
[runProperties addObject:dateAttribute];
NSAttributeDescription *idAttribute= [NSAttributeDescription new];
idAttribute.name = @"processID";
idAttribute.attributeType = NSInteger32AttributeType;
idAttribute.optional = NO;
idAttribute.defaultValue = @0;
[runProperties addObject:idAttribute];
NSPredicate *validationPredicate = [NSPredicate predicateWithFormat:@"SELF >= 0"];
NSString *validationWarning = @"Process ID < 0";
[idAttribute setValidationPredicates:@[validationPredicate]
withValidationWarnings:@[validationWarning]];
runEntity.properties = runProperties;
NSDictionary *localizationDictionary = @{@"Property/processID/Entity/Run" : @"Process ID",
@"Property/date/Entity/Run" : @"Date",
@"ErrorString/Process ID < 0" : @"Process ID must not be less than 0"};
model.localizationDictionary = localizationDictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment