我希望能够将托管对象上下文传递给UITableView视图,以便能够保存日期。我怎样才能做到这一点?我应该添加什么?我连接了模型,并使用实体Event和属性timeStamp对其进行了配置。这是项目,它非常简单,我认为不会花您一些时间,因为我除了NavigationBar添加按钮之外没有添加太多东西,其他都是基础知识。我确实确实试图使其工作,但出现一个错误,告诉我ManagedObjectContext传递的结果为nil。 https://www.dropbox.com/s/a1348diy589c2s0/Demo.zip?m
最佳答案
我检查了您的代码。您没有模型类e Event.h(m)
。首先,您必须创建它。
Demo.xcdatamodeld
选择您的实体Event
添加一个新的NSManagedObjectContext
文件子类#import "Event.h"
#import "DemoAppDelegate.h"
到您的DemoDateViewController.m NSManagedObjectContext *context = [(DemoAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
Event *event = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
[event setTimeStamp:[NSDate date]];
NSError *error = nil;
// If you have a propert called `managedObjectContext` assign `context` to it and use every where
// self.managedObjectContext = context
// or this is enough
if (![context save:&error])
{
NSLog(@"insertNewObject error = %@", error);
}