开发者

NSCoding protocol question

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


Try using NSKeyedArchiver instead of NSArchiver.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜