views:

137

answers:

2

I have a CoreData Mac app for which I'm writing an iOS front end. All's working well thusfar, I'm able to successfully read files created with the Mac app in the iOS version. I'm struggling with how to read the NSColor data (being stored as an NSData object in my data model) into a UIColor in iOS version of the app.

To summarize: How do take an NSData object containing an archived NSColor object and convert it to the appropriate UIColor object on an iOS device?

A: 

What I've done thus far (and it works, but isn't pretty) is to read the NSData into an NSString (the data seems to be ASCII encoded). The RGB values are present in the NSString and with a little parsing can be extracted to create a new UIColor.

I'm not sure if this is a global solution or one that happens to work in my case, but it's a start.

It would obviously be better to serialize as an NSArray and I'll look into that for the future.

Jeff Hellman
+1  A: 

Take a look at the following from the Core Data Programming Guide

Have you tried replacing NSColor with UIColor? UIColor implements the NSCoding protocol

// - (NSColor *)color
- (UIColor *)color
{

    [self willAccessValueForKey:@"color"];

    // NSColor *color = [self primitiveColor];
    UIColor *color = [self primitiveColor];

    [self didAccessValueForKey:@"color"];

     if (color == nil)

    {

        NSData *colorData = [self colorData];

        if (colorData != nil)

        {

            color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];                

            [self setPrimitiveColor:color];

        }

    }

    return color;

}
falconcreek
As best I can tell, this fails because UIKit is unable to unarchive and decode an object of Class NSColor.
Jeff Hellman
Ok then. Takea look at this SO question.http://stackoverflow.com/questions/2304882/core-data-data-model-attribute-type-for-uicolor
falconcreek
Perhaps I'm reading this wrong, but your linked question seems to show how to serialize and store a UIColor. This isn't my issue. I've got a serialized NSColor and I need to read it into a UIColor. Also, UIColor doesn't conform to NSCoder as stated in your answer...Thanks for the help though. This is really the only trouble spot I'm having reading a Mac OS Coredata file on iOS. Thus far.
Jeff Hellman
According to the docs UIColor conforms to the NSCoding protocol. http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIColor_Class/Reference/Reference.html. Do you have a sample NSColor that you want to have instantiated as a UIColor?
falconcreek