Skip to content
Browse files

Added category on NSManagedObject for easy converting between JSON-Di…

…ct and NSManagedObject
  • Loading branch information...
1 parent ea3240e commit e5d7385b094680c0bcee5bdfe73d57605084f410 @myell0w myell0w committed Jun 2, 2011
View
18 Categories/NSManagedObject/NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h
@@ -0,0 +1,18 @@
+//
+// NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h
+// PSFoundation
+//
+// Created by Matthias Tretter on 02.06.11.
+// Copyright 2011 @myell0w. All rights reserved.
+//
+// Taken from: http://www.cimgf.com/2011/06/02/saving-json-to-core-data/
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSManagedObject (MTSafeSetValuesForKeysWithDictionary)
+
+- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
+- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter;
+
+@end
View
56 Categories/NSManagedObject/NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m
@@ -0,0 +1,56 @@
+//
+// NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m
+// PSFoundation
+//
+// Created by Matthias Tretter on 02.06.11.
+// Copyright 2011 Peter Steinberger. All rights reserved.
+//
+// Taken from: http://www.cimgf.com/2011/06/02/saving-json-to-core-data/
+
+#import "NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h"
+
+
+@implementation NSManagedObject (MTSafeSetValuesForKeysWithDictionary)
+
+- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues {
+ NSDictionary *attributes = [[self entity] attributesByName];
+ for (NSString *attribute in attributes) {
+ id value = [keyedValues 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 = [[attributes 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];
+ }
+}
+
+- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter {
+ NSDictionary *attributes = [[self entity] attributesByName];
+ for (NSString *attribute in attributes) {
+ id value = [keyedValues objectForKey:attribute];
+ if (value == nil) {
+ continue;
+ }
+ NSAttributeType attributeType = [[attributes 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]];
+ } else if ((attributeType == NSDateAttributeType) && ([value isKindOfClass:[NSString class]]) && (dateFormatter != nil)) {
+ value = [dateFormatter dateFromString:value];
+ }
+ [self setValue:value forKey:attribute];
+ }
+}
+
+@end
View
8 PSFoundation.xcodeproj/project.pbxproj
@@ -36,6 +36,8 @@
39C27BD713146056000C0C94 /* UIView+Animation.m in Sources */ = {isa = PBXBuildFile; fileRef = 39C27BD513146056000C0C94 /* UIView+Animation.m */; };
39C27C0F131469A1000C0C94 /* NSString+Truncation.h in Headers */ = {isa = PBXBuildFile; fileRef = 39C27C0D131469A1000C0C94 /* NSString+Truncation.h */; };
39C27C10131469A1000C0C94 /* NSString+Truncation.m in Sources */ = {isa = PBXBuildFile; fileRef = 39C27C0E131469A1000C0C94 /* NSString+Truncation.m */; };
+ 39C917A4139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h in Headers */ = {isa = PBXBuildFile; fileRef = 39C917A2139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h */; };
+ 39C917A5139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 39C917A3139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m */; };
39D8262F12E364530079B785 /* UIView+Hierarchy.h in Headers */ = {isa = PBXBuildFile; fileRef = 39D8262D12E364530079B785 /* UIView+Hierarchy.h */; };
39D8263012E364530079B785 /* UIView+Hierarchy.m in Sources */ = {isa = PBXBuildFile; fileRef = 39D8262E12E364530079B785 /* UIView+Hierarchy.m */; };
39EBAAB113508EE100BBFB4B /* UIViewController+MTAnimatedFetch.h in Headers */ = {isa = PBXBuildFile; fileRef = 39EBAAAF13508EE100BBFB4B /* UIViewController+MTAnimatedFetch.h */; };
@@ -604,6 +606,8 @@
39C27BD513146056000C0C94 /* UIView+Animation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIView+Animation.m"; path = "Categories/UIView/UIView+Animation.m"; sourceTree = SOURCE_ROOT; };
39C27C0D131469A1000C0C94 /* NSString+Truncation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSString+Truncation.h"; path = "Categories/NSString/NSString+Truncation.h"; sourceTree = SOURCE_ROOT; };
39C27C0E131469A1000C0C94 /* NSString+Truncation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSString+Truncation.m"; path = "Categories/NSString/NSString+Truncation.m"; sourceTree = SOURCE_ROOT; };
+ 39C917A2139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h"; sourceTree = "<group>"; };
+ 39C917A3139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m"; sourceTree = "<group>"; };
39D8262D12E364530079B785 /* UIView+Hierarchy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIView+Hierarchy.h"; path = "Categories/UIView/UIView+Hierarchy.h"; sourceTree = SOURCE_ROOT; };
39D8262E12E364530079B785 /* UIView+Hierarchy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIView+Hierarchy.m"; path = "Categories/UIView/UIView+Hierarchy.m"; sourceTree = SOURCE_ROOT; };
39EBAAAF13508EE100BBFB4B /* UIViewController+MTAnimatedFetch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+MTAnimatedFetch.h"; sourceTree = "<group>"; };
@@ -1407,6 +1411,8 @@
children = (
78E53185126E03EB00A89047 /* NSManagedObject+AutoDescription.h */,
78E53186126E03EB00A89047 /* NSManagedObject+AutoDescription.m */,
+ 39C917A2139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h */,
+ 39C917A3139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m */,
);
name = NSManagedObject;
path = Categories/NSManagedObject;
@@ -1746,6 +1752,7 @@
7856A32612B43AA30025424F /* PSFoundation_CoreData.h in Headers */,
39EBAAB113508EE100BBFB4B /* UIViewController+MTAnimatedFetch.h in Headers */,
7816DC69135C4A17000AA9EF /* NSFetchedResultsController+ActiveRecord.h in Headers */,
+ 39C917A4139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2185,6 +2192,7 @@
78E5327B126E042100A89047 /* SafeFetchedResultsController.m in Sources */,
7837DCDD12A09381005CC81B /* NSManagedObjectContext+FetchedObjectFromURI.m in Sources */,
7816DC6A135C4A17000AA9EF /* NSFetchedResultsController+ActiveRecord.m in Sources */,
+ 39C917A5139832D10045489C /* NSManagedObject+MTSafeSetValuesForKeysWithDictionary.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
1 PSFoundation_CoreData.h
@@ -13,6 +13,7 @@
#import "NSManagedObjectContext+FetchedObjectFromURI.h"
#import "NSManagedObject+AutoDescription.h"
+#import "NSManagedObject+MTSafeSetValuesForKeysWithDictionary.h"
#import "CoreData+ActiveRecordFetching.h"

0 comments on commit e5d7385

Please sign in to comment.
Something went wrong with that request. Please try again.