我想将uiviewcontroller类保存到coredata中,并通过按钮单击从coredata中检索它。我已经尝试过nsarchiver和nsunarchiver。将类转换为nsdata并保存到coredata。但在检索时我得到了一个错误:
- [__ NSArrayI bytes]:无法识别的选择器发送到实例0x6d442a0,我的代码在下面
// the button press for saving the object
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewcontrollerobject];
ViewData *viewdetails=[NSEntityDescription insertNewObjectForEntityForName:@"ViewData" inManagedObjectContext:[self managedObjectContext]];
viewdetails.view = data;
viewdetails.order = [NSNumber numberWithInt:1];
[self.fetchedResultsController performFetch:nil];
//button press for retrieving the class
[self.fetchedResultsController performFetch:nil];
NSData *theData;
theData = [fetchedResultsController.fetchedObjects valueForKey:@"view"];
viewcontroller *newviewcontrollerobject;
newviewcontrollerobject=[NSKeyedUnarchiver unarchiveObjectWithData:theData];
[newviewcontrollerobject.view setFrame:CGRectMake(200, 200, 300, 150)];
[self.view addSubview:newviewcontrollerobject.view];
请帮我纠正此代码
答案 0 :(得分:3)
我强烈建议不要使用此应用程序设计。您应该使用Core Data来保存 Model 的数据,而不是任何 View ,更不用说 Controller 了。
答案 1 :(得分:0)
这行代码返回NSData对象的NSArray:
[fetchedResultsController.fetchedObjects valueForKey:@"view"];
所以这会奏效:
NSArray* theDataArray = [fetchedResultsController.fetchedObjects valueForKey:@"view"];
theData = [theDataArray lastObject];