diff --git a/NSManagedObject+safeSetValuesForKeysWithDictionary.h b/NSManagedObject+safeSetValuesForKeysWithDictionary.h index eae9dae..130fdf0 100644 --- a/NSManagedObject+safeSetValuesForKeysWithDictionary.h +++ b/NSManagedObject+safeSetValuesForKeysWithDictionary.h @@ -12,4 +12,6 @@ - (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter; +- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter mapping:(NSDictionary *)mapping; + @end diff --git a/NSManagedObject+safeSetValuesForKeysWithDictionary.m b/NSManagedObject+safeSetValuesForKeysWithDictionary.m index 043a271..45201d6 100644 --- a/NSManagedObject+safeSetValuesForKeysWithDictionary.m +++ b/NSManagedObject+safeSetValuesForKeysWithDictionary.m @@ -10,11 +10,18 @@ @implementation NSManagedObject (safeSetValuesForKeysWithDictionary) -- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter +- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter { + [self safeSetManagedValuesForKeysWithDictionary:keyedValues dateFormatter:dateFormatter mapping:nil]; +} + +- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter mapping:(NSDictionary *)mapping { NSDictionary *attributes = [[self entity] attributesByName]; for (NSString *attribute in attributes) { - id value = [keyedValues objectForKey:attribute]; + id value = nil; + if (mapping != nil) { value = [keyedValues objectForKey:[mapping objectForKey:attribute]]; } + else { value = [keyedValues objectForKey:attribute]; } + if (value == nil) { continue; } diff --git a/README.md b/README.md index 0a2d75d..3d18d88 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,21 @@ Some categories to map JSON to CoreData model Credit goes to original author, Tom Harrington : http://www.cimgf.com/2011/06/02/saving-json-to-core-data/ +# EXAMPLE +NSDictionary *mapping = @{ + @"name": @"countdownName", + @"date": @"countdownDate" + }; + +NSDictionary *values = @{ + @"countdownName": @"Hello", + @"countdownDate": @"10/29/2008 08:29PM" + }; + +Countdown *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Countdown" inManagedObjectContext:self.managedObjectContext]; + +[newObject safeSetManagedValuesForKeysWithDictionary:values dateFormatter:dateFormat mapping:mapping]; + ## TODO This category does not deal with relationships