2012-11-04 42 views
3

我創建了一個UITableViewController的故事板,然後添加了一個Core Data實體。在這一點上的應用程序構建和運行沒有錯誤,但UITableViewController沒有顯示數據。iOS - CoreData - TableViewController - NSInvalidArgumentException',reason:'+ entityForName:nil不合法NSManagedObjectContext

我刪除了TVC和故事板重修,但自從當我運行應用程序並嘗試打開TVC我得到一個錯誤:

*終止應用程序由於未捕獲的異常' NSInvalidArgumentException」的,理由是:‘+ entityForName:無不是 法律的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 
+0

使用此'的NSLog(@ 「%@」,managedObjectContext) ;'而不是'NSLog(managedObjectContext);'說出你在控制檯中看到的內容。然後,在你的'AppDelegate'中發佈核心數據棧的實現。 –

+0

這裏是日誌'2012-11-05 08:27:13.304 [9872:11603](null)' – user1797508

回答

1

這條線:

NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext]; 

您聲明一個局部managedObjectContext並賦予它,而不是你應該做的:

managedObjectContext = [appDelegate managedObjectContext]; 

這將使用TVC的iVar

+0

好點。我第一次沒有看到它。 + 1. –

+1

我很驚訝LLVM沒有提出有關局部變量影響全局變量的警告,這通常會引發這個問題。 – Abizern

+0

感謝Abizem的建議。我做了改變,但仍然有完全相同的問題。 – user1797508

0

所以我找到了問題所在。我沒有複製xcode生成示例中AppDelegate的所有類,因此定義managedObject和persistent store等的類不在那裏。

-1

在您的視圖控制器實現文件,對這段代碼下:

- (void)viewDidLoad 
{ 

添加這段代碼:

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

你能解釋一下爲什麼嗎? – Mark

相關問題