本文介绍了EXC_BAD_ACCESS在使用Core Data时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocoa新手,正在编写一个简单的应用程序来学习使用Core Data,但是它会与 EXC_BAD_ACCESS 崩溃。试了几件事情,还没有找到解决方案。

I'm new into Cocoa and am writing a simple app to learn working with Core Data, but it crashes with EXC_BAD_ACCESS. Tried several things and haven't find the solution yet. As I said, I'm not very experienced in Cocoa.

我按照通常的Core Data教程。

I have followed the usual Core Data tutorials.

这是我的模型:

我已将这两个实体添加到我的Nib文件中的 NSArrayController ,并且有两个 NSTableView 具有值绑定到实体对象。

I've added these two entities as NSArrayController in my Nib file and have two NSTableViews with Value Binding to the entity objects.

这里是代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSManagedObjectContext *context = [self managedObjectContext];
    TaskList *list = [NSEntityDescription
                      insertNewObjectForEntityForName:@"TaskList"
                      inManagedObjectContext: context]; // EXC_BAD_ACCESS happens here
    [list setTitle:@"Inbox"];
    Task *task = [NSEntityDescription
                  insertNewObjectForEntityForName:@"Task"
                  inManagedObjectContext: context];
    [task setKey:@"Remember the milk"];
    [task setList:list];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Error: %@", [error localizedDescription]);
    }
}

就是这样!这就是我的计划。我使用Xcode 4.2,开发一个Mac应用程序,并启用ARC。

That's it! That's all my program. I am using Xcode 4.2, developing a Mac app, and ARC is enabled.

更新: jrturton要求包括 [self managedObjectContext] 。我没有写这个代码,但这里是我发现在 AppDelegate.h

UPDATE: jrturton asked me to include implementation of [self managedObjectContext]. I didn't write this code, but here's what I found in AppDelegate.h:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

这是来自 AppDelegate.m

@synthesize managedObjectContext = __managedObjectContext;
...
/**
    Returns the managed object context for the application (which is already
    bound to the persistent store coordinator for the application.)
 */
- (NSManagedObjectContext *)managedObjectContext {
    if (__managedObjectContext) {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
        [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
        NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
    __managedObjectContext = [[NSManagedObjectContext alloc] init];
    [__managedObjectContext setPersistentStoreCoordinator:coordinator];

    return __managedObjectContext;
}


推荐答案

检查您的托管对象模型。确保所有实体和属性名称拼写正确。还要检查你的对象类文件,并确保它们包含你期望的。

Check your managed object model. Make sure all the entity and attribute names are spelled correctly. Also check your object class files and make sure they contain what you expect.

也许调试器不显示正确的行崩溃:我注意到,方法 setKey:,但在您的任务实体中没有调用的属性。尝试使用点符号设置所有属性,例如 list.title = @Inbox。 (这通常更容易阅读,并避免类似的错误。)

Maybe the debugger does not show you the correct row when crashing: I noticed, that you have a method setKey:, but no attribute called keyin your Task entity. Try setting all the attributes with the dot notation, like list.title = @"Inbox". (This is generally easier to read and avoids similar errors.)

根据建议,在插入新实体之前,设置断点并确保受管对象上下文不为空。

As suggested, before the line inserting the new entity, set a breakpoint and make sure the managed object context is not null.

最后,也许你必须投射你的对象。 insertNewObjectForEntityForName:返回类型为 NSManagedObject 的对象,但您要将其分配给 TaskList 。尝试在使用对象之前添加转换:

Finally, perhaps you have to cast your object. insertNewObjectForEntityForName: returns an object of type NSManagedObject, but you are assigning it to a type TaskList. Try adding the cast before you use the object:

TaskList *list = (TaksList *) [NSEntityDescription
                  insertNewObjectForEntityForName:@"TaskList"
                  inManagedObjectContext: context];

这篇关于EXC_BAD_ACCESS在使用Core Data时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:00