From 8fd06c278e711d4416fca62c074d62a269d63183 Mon Sep 17 00:00:00 2001 From: Anton Priestley Date: Thu, 13 Feb 2014 03:00:53 +0000 Subject: [PATCH 1/3] Added mapping dictionary with different key names By passing a dictionary of mapping values, the key values can have different from the Core Data object keys. --- NSManagedObject+safeSetValuesForKeysWithDictionary.h | 2 ++ NSManagedObject+safeSetValuesForKeysWithDictionary.m | 10 +++++++++- README.md | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) 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..e8f0b17 100644 --- a/NSManagedObject+safeSetValuesForKeysWithDictionary.m +++ b/NSManagedObject+safeSetValuesForKeysWithDictionary.m @@ -11,10 +11,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..c730278 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,18 @@ 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 From 8834e2ffdbe0826582f6aa708146246e1f3e83fd Mon Sep 17 00:00:00 2001 From: Anton Priestley Date: Thu, 13 Feb 2014 03:05:20 +0000 Subject: [PATCH 2/3] Mapping key value changes Mapping key value changes --- NSManagedObject+safeSetValuesForKeysWithDictionary.m | 1 - 1 file changed, 1 deletion(-) diff --git a/NSManagedObject+safeSetValuesForKeysWithDictionary.m b/NSManagedObject+safeSetValuesForKeysWithDictionary.m index e8f0b17..45201d6 100644 --- a/NSManagedObject+safeSetValuesForKeysWithDictionary.m +++ b/NSManagedObject+safeSetValuesForKeysWithDictionary.m @@ -10,7 +10,6 @@ @implementation NSManagedObject (safeSetValuesForKeysWithDictionary) -- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter - (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter { [self safeSetManagedValuesForKeysWithDictionary:keyedValues dateFormatter:dateFormatter mapping:nil]; } From 14c76f525931ecd904a7c149a5ab78e4996a8a9a Mon Sep 17 00:00:00 2001 From: Anton Priestley Date: Thu, 13 Feb 2014 03:06:36 +0000 Subject: [PATCH 3/3] Update readme Update readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index c730278..3d18d88 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,14 @@ 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