+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Account''

后端 未结 5 1605
日久生厌
日久生厌 2020-12-01 20:52

I have tried a lot of options, but can\'t find the solution for this problem. I created a Core Data file and named the entity Account, Created an string attribute called use

5条回答
  •  生来不讨喜
    2020-12-01 21:22

    - (NSManagedObjectContext *)managedObjectContext
    {
        if (managedObjectContext != nil) return managedObjectContext;
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
    
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator:coordinator];
        }
        return managedObjectContext;
    }
    
    • You haven't provided a lazy loading implementation of persistentStoreCoordinator
    • so coordinator will always be nil
    • so you will always be returning nil from this method
    • which means you will always get the error above.

    To explain the error:

    +entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Account'

    It's not immediately obvious from reading it, but this means that nil is not a legal thing to pass for the managed object context. On first reading, it looks like you're doing entityForName:nil but that isn't the case.

    To fix the problem, you will need to provide a valid persistent store coordinator. I have a small article here which explains just how little code you need to set up a core data stack, this may help you.

提交回复
热议问题