How to serialize object containing NSData?
- by AO
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_STRING];
self.d [coder decodeObjectForKey:KEY_DATA];
}
return self;
}
- (NSData*) serialize
{
return [NSKeyedArchiver archivedDataWithRootObject:self];
}
@end