Created
October 28, 2012 07:37
-
-
Save RobvH/3967979 to your computer and use it in GitHub Desktop.
Core Data transient attributes - how to add needed custom code to category to preserve autogeneration of managed object sub class files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Category.h" | |
@interface Category (Transient) | |
@property (nonatomic, strong) UIColor *color; | |
- (UIColor *)primitiveColor; | |
- (void)setPrimitiveColor:(UIColor *)value; | |
- (NSData *)primitiveColorData; | |
- (void)setPrimitiveColorData:(NSData *)value; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Category+Transient.h" | |
// this implementation will trigger a warning that it is incomplete, lacking definitions for primitiveColor | |
// setPrimitiveColor, primitiveColorData, setPrimitiveColorData | |
@implementation Category (Create) | |
@dynamic color; | |
- (UIColor *)color | |
{ | |
[self willAccessValueForKey:@"color"]; | |
UIColor *tempColor = [self primitiveColor]; | |
[self didAccessValueForKey:@"color"]; | |
if (tempColor) { | |
return tempColor; | |
} | |
NSData *tempColorData = [self colorData]; | |
if (!tempColorData) { | |
return nil; | |
} | |
tempColor = [NSKeyedUnarchiver unarchiveObjectWithData:tempColorData]; | |
[self setPrimitiveColor:tempColor]; | |
return tempColor; | |
} | |
- (void)setColor:(UIColor *)aColor { | |
[self willChangeValueForKey:@"color"]; | |
[self setPrimitiveValue:aColor forKey:@"color"]; | |
[self didChangeValueForKey:@"color"]; | |
[self setValue:[NSKeyedArchiver archivedDataWithRootObject:aColor] | |
forKey:@"colorData"]; | |
} | |
- (void)willSave | |
{ | |
UIColor *tempColor = [self primitiveColor]; | |
if ( tempColor ) { | |
[self setPrimitiveColorData:[NSKeyedArchiver archivedDataWithRootObject:tempColor]]; | |
} | |
else { | |
[self setPrimitiveColor:nil]; | |
} | |
[super willSave]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// autogenerated | |
#import <Foundation/Foundation.h> | |
#import <CoreData/CoreData.h> | |
@class SubReceipt; | |
@interface Category : NSManagedObject | |
@property (nonatomic, strong) NSString * name; | |
@property (nonatomic, strong) NSData * colorData; | |
//@property (nonatomic, strong) UIColor *color; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// autogenerated | |
#import "Category.h" | |
#import "SubReceipt.h" | |
@implementation Category | |
@dynamic name; | |
@dynamic colorData; | |
//@dynamic color; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Category.h" | |
@interface Category (Transient) | |
@property (nonatomic, strong) UIColor *color; | |
@property (nonatomic, strong) UIColor *primitiveColor; | |
@property (nonatomic, strong) NSData *primitiveColorData; | |
/* when used in a category, this will lead to incomplete implementation warnings | |
- (UIColor *)primitiveColor; | |
- (void)setPrimitiveColor:(UIColor *)value; | |
- (NSData *)primitiveColorData; | |
- (void)setPrimitiveColorData:(NSData *)value; | |
*/ | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Category+Transient.h" | |
// this implementation will trigger a warning that it is incomplete, lacking definitions for primitiveColor | |
// setPrimitiveColor, primitiveColorData, setPrimitiveColorData | |
@implementation Category (Create) | |
@dynamic color; | |
@dynamic primitiveColor; | |
@dynamic primitiveColorData; | |
- (UIColor *)color | |
{ | |
[self willAccessValueForKey:@"color"]; | |
UIColor *tempColor = [self primitiveColor]; | |
[self didAccessValueForKey:@"color"]; | |
if (tempColor) { | |
return tempColor; | |
} | |
NSData *tempColorData = [self colorData]; | |
if (!tempColorData) { | |
return nil; | |
} | |
tempColor = [NSKeyedUnarchiver unarchiveObjectWithData:tempColorData]; | |
[self setPrimitiveColor:tempColor]; | |
return tempColor; | |
} | |
- (void)setColor:(UIColor *)aColor { | |
[self willChangeValueForKey:@"color"]; | |
[self setPrimitiveValue:aColor forKey:@"color"]; | |
[self didChangeValueForKey:@"color"]; | |
[self setValue:[NSKeyedArchiver archivedDataWithRootObject:aColor] | |
forKey:@"colorData"]; | |
} | |
- (void)willSave | |
{ | |
UIColor *tempColor = [self primitiveColor]; | |
if ( tempColor ) { | |
[self setPrimitiveColorData:[NSKeyedArchiver archivedDataWithRootObject:tempColor]]; | |
} | |
else { | |
[self setPrimitiveColor:nil]; | |
} | |
[super willSave]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment