首页 文章

iOS - CoreData - TableViewController - NSInvalidArgumentException ', reason: ' entityForName:nil不是合法的NSManagedObjectContext

提问于
浏览
3

我用 UITableViewController 创建了一个故事板,然后添加了一个Core Data实体 . 此时应用程序构建并运行没有错误,但 UITableViewController 没有显示数据 .

我删除了TVC并在StoryBoard中重建,但是当我运行应用程序并尝试打开TVC时出现错误:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'Attractions''

通过一些研究,意识到这是由于我的 managedObjectContext 是空的,但对于我的生活,我无法弄清楚为什么它是空的 .

在TVC头文件中:

#import <UIKit/UIKit.h>
#import "Attractions.h"
#import "AttractionListViewCell.h"
#import "ApplicationNameAppDelegate.h"

@interface AttractionListViewController : UITableViewController
{
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *AttractionsArray;    
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *AttractionsArray;

- (void) fetchrecords;

@end

在TVC模型文件中:

ApplicationNameAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

NSLog(managedObjectContext);
// Create connection to the DB via Context
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Attractions" inManagedObjectContext:managedObjectContext];

在ApplicationNameAppDelegate.h文件中:

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

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

您可以提供的任何帮助或见解将非常感激 .

编辑 - 添加了AppDelegate信息:

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

@class AttractionListViewController;

@interface AppNameAppDelegate : UIResponder <UIApplicationDelegate>
{
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
}

@property (strong, nonatomic) AttractionListViewController *viewController;
@property (strong, nonatomic) UIWindow *window;

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

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

3 回答

  • 0

    这一行:

    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    

    您正在声明本地managedObjectContext并分配它而不是您应该执行的操作:

    managedObjectContext = [appDelegate managedObjectContext];
    

    这将使用TVC的iVar

  • -1

    所以我发现了问题所在 . 我没有在xcode生成的示例中复制AppDelegate中的所有类,因此定义了managedObject和持久存储等的类不存在 .

  • 1

    在View Controller实现文件中,就在这段代码下面:

    - (void)viewDidLoad
    {
    

    添加这段代码:

    id delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = [delegate managedObjectContext];
    

相关问题