mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 17:32:18 +08:00
adding json helpers, start file renaming
This commit is contained in:
@@ -20,4 +20,6 @@
|
||||
#import "NSManagedObjectContext+ActiveRecord.h"
|
||||
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
|
||||
#import "NSManagedObjectModel+ActiveRecord.h"
|
||||
#import "NSPersistentStore+ActiveRecord.h"
|
||||
#import "NSPersistentStore+ActiveRecord.h"
|
||||
|
||||
#import "NSManagedObject+JSONHelpers.h"
|
||||
15
NSManagedObject+JSONHelpers.h
Normal file
15
NSManagedObject+JSONHelpers.h
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// NSManagedObject+JSONHelpers.h
|
||||
// Gathering
|
||||
//
|
||||
// Created by Saul Mora on 6/28/11.
|
||||
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
|
||||
//
|
||||
|
||||
#import <CoreData/CoreData.h>
|
||||
|
||||
@interface NSManagedObject (NSManagedObject_JSONHelpers)
|
||||
|
||||
- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData;
|
||||
|
||||
@end
|
||||
56
NSManagedObject+JSONHelpers.m
Normal file
56
NSManagedObject+JSONHelpers.m
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// NSManagedObject+JSONHelpers.m
|
||||
// Gathering
|
||||
//
|
||||
// Created by Saul Mora on 6/28/11.
|
||||
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSManagedObject+JSONHelpers.h"
|
||||
|
||||
|
||||
static NSString * const kNSManagedObjectAttributeJSONKeyMapKey = @"jsonKeyName";
|
||||
|
||||
@implementation NSString (JSONParsingHelpers)
|
||||
|
||||
- (NSDate *) ga_dateFromJSONString;
|
||||
{
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
[formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'Z'"];
|
||||
|
||||
return [formatter dateFromString:self];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@implementation NSManagedObject (NSManagedObject_JSONHelpers)
|
||||
|
||||
|
||||
- (void) setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData
|
||||
{
|
||||
NSDictionary *attributes = [[self entity] attributesByName];
|
||||
|
||||
for (NSString *attributeName in attributes)
|
||||
{
|
||||
NSAttributeDescription *attributeInfo = [attributes valueForKey:attributeName];
|
||||
|
||||
NSString *lookupKey = [[attributeInfo userInfo] valueForKey:kNSManagedObjectAttributeJSONKeyMapKey] ?: attributeName;
|
||||
id value = [jsonData valueForKey:lookupKey];
|
||||
|
||||
if (value == nil || [value isEqual:[NSNull null]])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
NSAttributeType attributeType = [attributeInfo attributeType];
|
||||
if (attributeType == NSDateAttributeType)
|
||||
{
|
||||
value = [value ga_dateFromJSONString];
|
||||
}
|
||||
|
||||
[self setValue:value forKey:attributeName];
|
||||
}
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user