views:

51

answers:

1

I want to add the archiving (NSCoding) protocol to my model class, and then i implement both methods encodeWithCoder:(NSCoder*)coder and initWithCoder:(NSCoder*)coder. MyModelClass has 2 instance variables (NSString and NSImage), so i use the encodeObject:(id)object forKey:(NSString*)string method to encode the object plus the value for particular key. But i keep got the error :

*** -encodeObject:forKey: only defined for abstract class. Define -[NSArchiver encodeObject:forKey:]!

here's my code for NSCoding methods :

- (id)initWithCoder:(NSCoder *)coder {
 [super init];
 mainPath = [[coder decodeObjectForKey:@"mainPath"] retain];
 icon = [[coder decodeObjectForKey:@"icon"] retain];

 return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
 NSLog(@"encode with coder is called");
 [coder encodeObject:mainPath forKey:@"mainPath"];
 [coder encodeObject:icon forKey:@"icon"];

}

And this is how i call them at my controller class :

id object = [assetArray objectAtIndex: [[rows lastObject] intValue]];

 if ([object isKindOfClass:[ItemAssetModel class]])
  NSLog(@"object is correct");
 else
  return NO;

 NSData *data = [NSArchiver archivedDataWithRootObject: object];

if i change the encodeObject:(id)obj forKey:(NSString*)str with encodeObject:(id)obj, the error is stops, but the result is, the archived data does not copy the instance variable value (cmiiw). Do i miss something on this?

thanks. hebbian

+4  A: 

Try using NSKeyedArchiver instead of NSArchiver.

aBitObvious
Right, because you're using keyed encoding/decoding methods.
David Gelhar
Ah, so that easy, thanks
Hebbian