1

我检查了其他一些线程,但找不到答案。

我正在关注有关核心数据的教程,并尝试将其应用于我的实用程序应用程序。

以下代码在应用程序委托中:

- (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)

这与托管对象上下文出现为零的翻转视图控制器.m 文件有关。我不明白为什么会这样。任何帮助将不胜感激。

干杯。

4

1 回答 1

0

didFinishLaunchingWithOptions:调用发生在翻转视图控制器加载之前。由于在 a 上调​​用方法时,Objective 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];
}
于 2013-09-25T01:41:12.953 回答