2012-06-23 47 views
0

我正在使用一個簡單的核心數據應用程序,該應用程序使用tableview和detail視圖。我收到錯誤消息,指出在對象類型ChildrenTVC中找不到屬性managedObjectContext。問題在於它確實存在。我已經清理了該項目並刪除了派生數據。必須有其他事情正在進行。 這裏是目標首標的代碼:在對象類型中找不到屬性

@interface ChildrenTVC : CoreDataTableViewController <AddChildTVCDelegate> 

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController; 

@end 

和用於執行文件的代碼:

#import "ChildrenTVC.h" 


@implementation ChildrenTVC 

@synthesize managedObjectContext = _managedObjectContext; 
@synthesize fetchedResultsController = _fetchedResultsController; 

這裏是應用程序代理文件,其中的錯誤寄存器:

#import "AppDelegate.h" 
#import "ChildrenTVC.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 
ChildrenTVC *controller = (ChildrenTVC *)navigationController.topViewController; 
controller.managedObjectContext = self.managedObjectContext; 
return YES; 
} 

錯誤在controller.managedObjectContext。很多這是樣板代碼,所以沒有太多。我只是不知道爲什麼它沒有看到託管對象上下文的屬性。該屬性在代碼中。

更新:

我最終重新創建了文件。我不確定在更改文件時仍然存在哪些引用,但有些內容指向錯誤的文件。我必須清空垃圾才能使新文件正常工作。儘管如此,它似乎現在可以工作。我所做的所有研究結果都揭示了Xcode中有些事情是無法解釋的。

+0

代碼似乎很好..嘗試清理您的項目,如果可能的話再次重新啓動xcode。 –

+0

我清理了項目並重新啓動了Xcode。錯誤仍然存​​在。這是一個謎。 – attentionjay

+0

可以在這裏輸入確切的錯誤? –

回答

1

我已經通過完全重新創建頭文件解決了這個問題,會發生什麼,但我從來沒有真正理解爲什麼會發生。那時我不明白的是我對應用程序所做的更改造成的影響。真正的問題是導入頭文件的順序問題。我更改了兩個實現文件中的#import,導致編譯器不讀取其中一個頭文件。我可以看到代碼在那裏,但編譯器無法讀取代碼,因爲它沒有導入代碼。這讓位於我收到的錯誤。我的解決方案簡單地逆轉了我所做的改變。通過簡單地刪除應用程序委託上的視圖控制器頭文件的#import就可以解決問題。我最近測試了這個,這是正確的解決方案。

0

你是類型鑄造navigationController.topViewController,但我認爲它不是現實中的一個。試試這個

ChildrenTVC *controller = [[ChildrenTVC alloc] init]; 
NSArray *vcArray = NSArray *vcArray = [NSArray arrayWithObject:controller]: 
[self.window.rootViewController setViewControllers:vcArray animated:NO]; 
+0

我試過這個,但它返回了另一個錯誤「賦值到只讀屬性」,我仍然有以前的錯誤。必須有其他事情發生。 – attentionjay

+0

糾正我的答案。 topViewController是隻讀屬性。要將viewcontrollers添加到'UINavigationController',你必須使用'setViewControllers:animated:' – Pfitz

+0

@Pfitz這是一個編譯器錯誤而不是運行時異常。 –

-2

,如果你做如下修改ChildrenTVC.h

@interface ChildrenTVC : CoreDataTableViewController <AddChildTVCDelegate> 
{ 
    NSManagedObjectContext *managedObjectContext; 
    NSFetchedResultsController *fetchedResultsController; 
} 
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController; 

@end 
+1

不應該有所作爲。 – Vervious

+0

添加此代碼沒有變化。 – attentionjay

+0

對不起,它沒有工作。我很高興你找到了解決方法。 – BumbleBoks

相關問題