Revision: 48176
Updated Code
at June 26, 2011 07:25 by mattneary
Updated Code
NSManagedObjectModel *mom = [self managedObjectModel]; NSDictionary *attrs = [[[mom entitiesByName] objectForKey:@"Course"] attributesByName]; NSArray *keyedValues = [[NSArray alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"courses" ofType:@"plist"] ]; for( NSDictionary *keyedValueDict in keyedValues ) { NSManagedObject *mObj = [NSEntityDescription insertNewObjectForEntityForName:@"Course" inManagedObjectContext:[self managedObjectContext]]; for (NSString *attribute in attrs) { id value = [keyedValueDict objectForKey:attribute]; if (value == nil) { // Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues continue; } NSAttributeType attributeType = [[attrs objectForKey:attribute] attributeType]; if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) { value = [value stringValue]; } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) { value = [NSNumber numberWithInteger:[value integerValue]]; } else if ((attributeType == NSFloatAttributeType) && ([value isKindOfClass:[NSString class]])) { value = [NSNumber numberWithDouble:[value doubleValue]]; } [mObj setValue:value forKey:attribute]; NSLog(@"Value %@ for Key %@", value, attribute); } } NSError *error; [[self managedObjectContext] save:&error];
Revision: 48175
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 26, 2011 07:20 by mattneary
Initial Code
NSManagedObjectModel *mom = [self managedObjectModel]; NSDictionary *attrs = [[[mom entitiesByName] objectForKey:@"Course"] attributesByName]; NSArray *keyedValues = [[NSArray alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"courses" ofType:@"plist"] ]; for( NSDictionary *keyedValueDict in keyedValues ) { NSManagedObject *mObj = [NSEntityDescription insertNewObjectForEntityForName:@"Course" inManagedObjectContext:[self managedObjectContext]]; for (NSString *attribute in attrs) { id value = [keyedValueDict objectForKey:attribute]; if (value == nil) { // Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues continue; } NSAttributeType attributeType = [[attrs objectForKey:attribute] attributeType]; if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) { value = [value stringValue]; } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) { value = [NSNumber numberWithInteger:[value integerValue]]; } else if ((attributeType == NSFloatAttributeType) && ([value isKindOfClass:[NSString class]])) { value = [NSNumber numberWithDouble:[value doubleValue]]; } //[self setValue:value forKey:attribute]; [mObj setValue:value forKey:attribute]; NSLog(@"Value %@ for Key %@", value, attribute); } } NSError *error; [[self managedObjectContext] save:&error];
Initial URL
Initial Description
Just change the filename "courses" to that of your own and the entity name "Course" to your own.
Initial Title
Core Data Import from Plist
Initial Tags
data
Initial Language
Objective C