Saving custom class into Coredata

眉间皱痕 提交于 2019-12-21 05:14:17

问题


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

But it says

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'

Here is my code

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 object is my custom class and im trying to create an object and then store it core data .Could you please help me out.

Thanks Dheeraj


回答1:


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. 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.

  2. 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.



来源:https://stackoverflow.com/questions/16305537/saving-custom-class-into-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!