3

I am using a custom NSValueTransformer to store color information in my Core Data store. The transformation between Transformable data and a UIColor instance works great once the color data is in the store already (ie once the app has been run and quit once already). However when I first run the app and am loading in these values (from a text file) they "stuck" as NSCFStrings.

In this line of code "attributes" is a dictionary has keys which are NSManagedObject attribute names and values that are the expected values for those attributes. In my color example the key value pair is "color":"1,1,1,0.5"

[object setValue:[attributes valueForKey:attribute] forKey:attribute];

The value for "color" will now remain a string in this instance until it's get transformed via my NSValueTransformer and then retransformed into a UIColor when the app gets run again.

I could just do the same transform here that I'm doing in the NSValueTransformer, but this is in a utility class I wrote that could theoretically be used for any transformer. I also thought of finding a way to get all newly created NSManagedObject instances out fo memory thereby forcing the transformation to go through, but that just seems like a hack.

Note: This "hack" works for me and let's me continue, but still feels ugly. Use NSManagedObjectContext's reset method if you're having similar problems / looking for a "just work" solution.

Any ideas?

(I have a hunch this is similar to " Why is my transformable Core Data attribute not using my custom NSValueTransformer? " but outside of the title our problems seem to be different)

Here is my NSValueTransformer

@implementation UIColorRGBValueTransformer

+ (Class)transformedValueClass
{
    return [NSData class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    return [value dataUsingEncoding:NSUTF8StringEncoding];
}

- (id)reverseTransformedValue:(id)value
{
    NSString *colorAsString = [[[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding] autorelease];
    NSArray *components = [colorAsString componentsSeparatedByString:@","];
    CGFloat r = [[components objectAtIndex:0] floatValue];
    CGFloat g = [[components objectAtIndex:1] floatValue];
    CGFloat b = [[components objectAtIndex:2] floatValue];
    CGFloat a = [[components objectAtIndex:3] floatValue];
    return [UIColor colorWithRed:r green:g blue:b alpha:a];

    return nil;
}

@end
Community
  • 1
  • 1
rob5408
  • 2,972
  • 2
  • 40
  • 53
  • Clarification: What do you mean by "...they "stuck" as NSCFStrings." Do you mean you only get string values until you save the context? – TechZen Jun 10 '11 at 14:00
  • Yes, the values are of type NSCFString during the first execution, but are then UIColors from then on. – rob5408 Jun 10 '11 at 16:38

1 Answers1

0

You're really not using the transformable attribute as intended. Your method should work in theory but it is an ugly kludge.

You usually only have to write a custom value transformer for a system defined class when you want to do something non-standard. In the vast majority cases, the preferred method is to use the default NSKeyedUnarchiveFromDataTransformerName. Any class that implements the NSCoding protocol can use that value transformer.

Since UIColor does implement the NSCoding protocol, you can just set the attribute to 'transformable' and the NSKeyedUnarchiveFromDataTransformerName will populate the transformer name automatically (at least it did in Xcode 3.x.) In use, Core Data will create the appropriate accessors so you can set and get the UIColor attribute just like any other key:

[aMoObject setValue:aUIcolorObj forKey:@"colorAttributeName"];

As a good rule of thumb, the API will do most of the work for you in the case of API classes. If you find yourself working hard with a system class, you've probably missed something.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Thanks for the response. I got off track originally because an answer here " http://stackoverflow.com/questions/2304882/core-data-data-model-attribute-type-for-uicolor " explicitly said UIColor does not conform to NSCoding. Unfortunately I never thought to check that "fact" for myself. Somehow this all did end up working for me because of where the original data for the colors originated from (1,1,1,0.5 in the text file). I think I'll remove the transformer and figure out a slick way to store a UIColor as text in my json file and have the system pick it up and store as such. – rob5408 Jun 10 '11 at 16:45