我检查过其他一些帖子但找不到答案。
我正在关注Core Data的教程,我正在尝试将其应用到我的实用程序应用程序。
以下代码位于应用程序委托中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
MessageDetails *messageDetails = [NSEntityDescription
insertNewObjectForEntityForName:@"MessageDetails"
inManagedObjectContext:context];
messageDetails.to = @"0861234567";
messageDetails.message = @"Test Message";
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
//Lists out all the objects currently in the database:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MessageDetails" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (MessageDetails *details in fetchedObjects) {
NSLog(@"To: %@", details.to);
NSLog(@"Message: %@", details.message);
}
// Override point for customization after application launch.
CCCMainViewController *controller = (CCCMainViewController *)self.window.rootViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
我的侧视图控制器具有以下代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MessageDetails" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.allMessageDetails = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
}
我收到以下错误:
2013-09-25 02:09:11.238 SendMessageV2[9267:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'MessageDetails''
*** First throw call stack:
(0x301fdf53 0x3a5d46af 0x2ff5332b 0x25345 0x32973603 0x329733c1 0x32a1d637 0x32a59d7b 0x32a586b3 0x32a57705 0x32c3265b 0x329a3f3f 0x329a3edf 0x329a3eb9 0x3298fb3f 0x329a392f 0x329a3601 0x3299e68d 0x32973a25 0x32972221 0x301c918b 0x301c865b 0x301c6e4f 0x30131ce7 0x30131acb 0x34e52283 0x329d3a41 0x23cc9 0x3aadcab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这与flipside视图controller.m文件有关,其中托管对象上下文将以nil形式出现。我不明白为什么会这样。任何帮助将不胜感激。
干杯。
答案 0 :(得分:0)
didFinishLaunchingWithOptions:
调用在加载翻转视图控制器之前发生。由于目标C在nil
上调用方法时不介意,因此调用
controller.managedObjectContext = self.managedObjectContext;
成功而没有警告。
不要试图将上下文“推”到您的侧视图控制器上,而是尝试从viewDidLoad
方法“拉”它,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Add this line:
managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MessageDetails" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.allMessageDetails = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
}