Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NSManagedObject+safeSetValuesForKeysWithDictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter;

- (void)safeSetManagedValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter mapping:(NSDictionary *)mapping;

@end
11 changes: 9 additions & 2 deletions NSManagedObject+safeSetValuesForKeysWithDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down