title: How to serialize object containing NSData?
body: <p>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 <a href="http://www.isolated.se" rel="nofollow noreferrer"><a href="http://www.isolated.se" rel="nofollow noreferrer">http://www.isolated.se</a></a> but my code (see below) results in the error "[NSConcreteData data]: unrecognized selector sent to instance...". How do I serialize my object?</p>
  
  <p>Header file:</p>
  
  <pre><code>@interface Donkey : NSObject&lt;NSCoding&gt;
  {
     NSString* s;
     NSData* d;
  }
  
  @property (nonatomic, retain) NSString* s;
  @property (nonatomic, retain) NSData* d;
  
  - (NSData*) serialize;
  
  @end
  </code></pre>
  
  <p>Implementation file:</p>
  
  <pre><code>@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
  </code></pre>