views:

216

answers:

1

I'm trying to serialize an object containing a number of data fields...where one of the fields is of datatype NSData which won't serialize. I've followed instructions at http://www.isolated.se but my code (see below) results in the error "[NSConcreteData data]: unrecognized selector sent to instance...". How do I serialize my object?

Header file:

@interface Donkey : NSObject<NSCoding>
{
   NSString* s;
   NSData* d;
}

@property (nonatomic, retain) NSString* s;
@property (nonatomic, retain) NSData* d;

- (NSData*) serialize;

@end

Implementation file:

@implementation Donkey

@synthesize s, d;

static NSString* const KEY_S = @"string";
static NSString* const KEY_D = @"data";

- (void) encodeWithCoder:(NSCoder*)coder
{
    [coder encodeObject:self.s forKey:KEY_S];
    [coder encodeObject:self.d forKey:KEY_D];
}

- (id) initWithCoder:(NSCoder*)coder;
{
    if(self = [super init])
    {
        self.s = [coder decodeObjectForKey:KEY_S];
        self.d = [coder decodeObjectForKey:KEY_D];
    }

    return self;
}

- (NSData*) serialize
{
    return [NSKeyedArchiver archivedDataWithRootObject:self];
}

@end
A: 

Your problem is most likely a naming collision caused by using data as a property name because otherwise the code looks fine and both NSString and NSData should serialize easily.

Try refactoring data into something like "theData" or "myData" and see if the issue resolves.

TechZen
I made some updates above according to your suggestions but the issue remains. I also forgot to mention that I serialize the object to NSData with the serialize method.
AO
Okay, then you need to track it down the error down to the exact line where it fails and then post the code around that line. The code you have up now should work so your problem lays elsewhere.
TechZen