How to serialize object containing NSData?

Posted by AO on Stack Overflow See other posts from Stack Overflow or by AO
Published on 2010-04-09T13:24:16Z Indexed on 2010/04/09 13:43 UTC
Read the original article Hit count: 417

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about serialize