Skip to content

Instantly share code, notes, and snippets.

@RobvH
Created October 28, 2012 07:37
Show Gist options
  • Save RobvH/3967979 to your computer and use it in GitHub Desktop.
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
#import "Category.h"
@interface Category (Transient)
@property (nonatomic, strong) UIColor *color;
- (UIColor *)primitiveColor;
- (void)setPrimitiveColor:(UIColor *)value;
- (NSData *)primitiveColorData;
- (void)setPrimitiveColorData:(NSData *)value;
@end
#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
// 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
// autogenerated
#import "Category.h"
#import "SubReceipt.h"
@implementation Category
@dynamic name;
@dynamic colorData;
//@dynamic color;
@end
#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
#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