将自定义类保存到Coredata中 [英] Saving custom class into Coredata

查看:135
本文介绍了将自定义类保存到Coredata中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过可转换的属性类型,并且在论坛之一中遵循了存档过程。

Hi i have used attribute type transformable and i have followed the procedure of archiving in one of the Forum .

但是它说

CoreData:警告:找不到属性的类名称为 SurveyResults的NSValueTransformer对实体'SurveyData'进行调查
2013-04-30 09:44:16.022 TestReachability [11641:c07]-[SurveyApplication encodeWithCoder:]:无法识别的选择器已发送至实例0x845cb00
2013-04-30 09 :44:16.023 TestReachability [11641:c07] * 由于未捕获的异常'NSInvalidArgumentException'终止了应用程序,原因:'-[SurveyApplication encodeWithCoder:]:无法识别的选择器发送到实例0x845cb00'

CoreData: warning: no NSValueTransformer with class name 'SurveyResults' was found for attribute 'survey' on entity 'SurveyData' 2013-04-30 09:44:16.022 TestReachability[11641:c07] -[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00 2013-04-30 09:44:16.023 TestReachability[11641:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00'

这是我的代码

SurveyApplication *survey =[[SurveyApplication alloc]init];
survey.name=@"dheeraj";
survey.phone=@"573-356-2598";

NSManagedObject *aStory = [NSEntityDescription insertNewObjectForEntityForName:@"SurveyData"inManagedObjectContext:self.managedObjectContext];
[aStory setValue:survey forKey:@"survey"];
NSError *saveError = [[NSError alloc]init];
if (![self.managedObjectContext save:&saveError]) {
    NSLog(@"%@",saveError);
}

SurveyAppplication对象是我的自定义类,我试图创建一个对象然后存储

SurveyAppplication object is my custom class and im trying to create an object and then store it core data .Could you please help me out.

感谢
Dheeraj

Thanks Dheeraj

推荐答案

仅使属性可转换还不够,您还需要安排转换的发生。您不能仅仅告诉Core Data转换任何旧对象并期望它知道该怎么做。您有两种选择:

It's not enough to make the attribute transformable, you also need to arrange for the transformation to happen. You can't just tell Core Data to transform any old object and expect it to know what to do. You have a couple of options:


  1. 不要告诉Core Data如何转换数据。在这种情况下,Core Data将尝试在您的对象上调用 encodeWithCoder:将其转换为 NSData 。这就是为什么您会收到提及此方法的错误的原因-它试图在您的类上调用该方法,但该方法不存在。在这种情况下,您的类必须符合 NSCoding 才能进行转换。

  1. Don't tell Core Data how to transform the data. In this case, Core Data will attempt to call encodeWithCoder: on your object to convert it to NSData. That's why you get the error that mentions this method-- it's trying to call the method on your class, but that method doesn't exist. In this case your class must conform to NSCoding for the transformation to occur.

告诉核心数据如何转换数据。在这种情况下,您将创建一个 NSValueTransformer 的子类来执行转换。您可以在核心数据模型编辑器或代码中的属性上配置此属性。在这种情况下,您必须具有一个自定义转换器类,该类知道如何执行转换。

Tell Core Data how to transform the data. In this case you create a subclass of NSValueTransformer that performs the transformation. You configure this on the attribute, either in the Core Data model editor or in code. In this case, you must have a custom transformer class that knows how to perform the transformation.

这篇关于将自定义类保存到Coredata中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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