【问题标题】:iOS - CoreData - TableViewController - NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContextiOS - CoreData - TableViewController - NSInvalidArgumentException',原因:'+entityForName: nil 不是合法的 NSManagedObjectContext
【发布时间】:2012-11-04 04:04:56
【问题描述】:

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

我删除了 TVC 并在 StoryBoard 中重建,但自从我在运行应用程序并尝试打开 TVC 时遇到错误:

* 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'+entityForName: nil is not a 搜索实体名称的合法 NSManagedObjectContext 参数 “景点”

通过一些研究,意识到这是由于我的 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

【问题讨论】:

  • 使用 NSLog(@"%@", managedObjectContext); 而不是 NSLog(managedObjectContext); 并说出您在控制台中看到的内容。然后,在您的AppDelegate 中发布 Core Data 堆栈的实现。
  • 这是日志'2012-11-05 08:27:13.304 [9872:11603] (null)'

标签: ios core-data uitableview


【解决方案1】:

这一行:

NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

您正在声明一个本地 managedObjectContext 并分配它,而不是您应该做什么:

managedObjectContext = [appDelegate managedObjectContext];

将使用 TVC 的 iVar

【讨论】:

  • 我很惊讶 LLVM 没有发出关于局部变量遮蔽全局变量的警告,这通常会解决这个问题。
  • 感谢 Abizem 的建议。我进行了更改,但仍然遇到完全相同的问题。
  • 哪行代码抛出错误?你有一个名为景点的实体吗?您的 FetchedResultsController 设置正确吗?
  • 就是这行代码 'NSEntityDescription *entity = [NSEntityDescription entityForName:@"Attractions" inManagedObjectContext:managedObjectContext]; ' 是的,实体 Attractions 已设置。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    在您的视图控制器实现文件中,就在这段代码下面:

    - (void)viewDidLoad
    {
    

    添加这段代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2015-07-06
      • 2014-09-28
      • 2013-12-26
      相关资源
      最近更新 更多