核心数据崩溃:[__ NSArrayM insertObject:atIndex:]:对象不能为零

sai*_*onx 10 iphone core-data objective-c ios

我试图在我的应用程序中实现CoreData来存储一个小型数据库.

在这里我的实施:

AppDelegate.h

#import <UIKit/UIKit.h>
#import "FavoritosViewController.h"
#import <CoreData/CoreData.h>

@interface XXX : NSObject <UIApplicationDelegate>{

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;



}
- (NSString *)applicationDocumentsDirectory;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    FavoritosViewController *global=[[FavoritosViewController alloc]init];

    global.managedObjectContext=[self managedObjectContext];

    .
    .
    .
    }

        - (void)applicationWillTerminate:(UIApplication *)application
    {
        NSError *error = nil;
        if (managedObjectContext != nil) {
            if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
                /*
                 Replace this implementation with code to handle the error appropriately.

                 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
                 */
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            } 
        }

    }

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}


/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FavoritosDatabase.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible
         * The schema for the persistent store is incompatible with current managed object model
         Check the error message to determine what the actual problem was.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}




- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Run Code Online (Sandbox Code Playgroud)

我还有一个xcdatamodeld,其中包含带有theirs属性的"Event"实体,以及来自它的Event.h,Event.m.

在FavoritosViewController我也有所有方法,但问题出现之前.

它来了

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
Run Code Online (Sandbox Code Playgroud)

应用程序崩溃,它显示如下:

*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [__ NSArrayM insertObject:atIndex:]:object不能为nil'

有任何想法吗???谢谢!!!

jca*_*d55 28

当我的代码运行[NSManagedObjectModel mergedModelFromBundles:nil]时,我遇到了类似的问题.这发生在xCode意外地撞到我之后.即使我恢复到我的代码的已知良好版本,我仍然有同样的错误,这是由于某种腐败.

经过多次试验,我能够通过退出Xcode和iPhone模拟器来解决问题,然后从以下目录中删除所有文件:

$ cd /Users/john/Library/Developer/Xcode/DerivedData

$ rm -R -f ./(folder corresponding to my project name)

$ cd /Users/john/Library/Application Support/iPhone Simulator/5.0/Applications

$ rm -R *
Run Code Online (Sandbox Code Playgroud)

这能够清除模拟器中损坏的临时文件和状态,并且错误消失了.


Jaa*_*tum 6

我将Model.xcdatamodeld文件移动到另一个文件夹,并出现此错误.从干净的模拟器开始没有帮助.显然,Xcode会在某个地方保留对此文件的引用.

我的修复是备份我的旧Model.xcdatamodeld文件,从项目中删除它,在同一文件夹中创建一个新的模型文件,然后用备份替换此文件.