NSCoding协议问题 [英] NSCoding protocol question

查看:156
本文介绍了NSCoding协议问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将存档(NSCoding)协议添加到我的模型类中,然后实现两种方法 encodeWithCoder:(NSCoder *)coder initWithCoder:(NSCoder *)coder 。 MyModelClass具有2个实例变量(NSString和NSImage),所以我使用 encodeObject:(id)object forKey:(NSString *)string 方法对对象加上值进行编码特定的键。但是我一直看到错误:

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:仅为抽象类定义。定义-[NSArchiver encodeObject:forKey:]!

这是我的NSCoding方法代码:

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];

如果我更改 encodeObject:(id)obj forKey:(NSString * )str encodeObject:{id)obj ,错误停止,但结果是,归档数据未复制实例变量值(cmiiw)。我想念这个吗?

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?

谢谢。
hebbian

thanks. hebbian

推荐答案

尝试使用NSKeyedArchiver而不是NSArchiver。

Try using NSKeyedArchiver instead of NSArchiver.

这篇关于NSCoding协议问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆