2011-07-16 87 views
1

我的應用程序是一個典型的基於非文檔的核心數據應用程序。我沒有修改任何預先提供的項目代碼,並且一直使用Xcode默認提供的AppDelegate文件(該項目不夠大,不足以保證更多)。manageObjectContext拋出無法識別的選擇器錯誤?

現在,當我嘗試將項目添加到我的核心數據存儲之一,

NSManagedObject *colorItemObject = [NSEntityDescription insertNewObjectForEntityForName:@"ColorItem" inManagedObjectContext: [NSApp managedObjectContext]]; 
    [colorItemObject setValue:[colorSetsArrayController valueForKeyPath:@"selection.colorSetName"] forKey:@"parent"]; 

它拋出一個無法識別的選擇異常:出現

-[_NSStateMarker managedObjectContext]: unrecognized selector sent to instance 0x100118290 

類似的錯誤,使用[自managedObjectContext ]。什麼可能導致這個?

回答

0

我敢肯定

[NSApp managedObjectContext] 

不會返回一個有效managedObjectContext。您是否使用managedObjectContext方法將類別設置爲NSApplication?如果不是,請將其更改爲

[self managedObjectContext] 

它應該工作。 (我認爲你的代碼是在你的應用程序委託中。)

+0

感謝您的回覆,但我也嘗試過[[NSApp委託] managedObjectContext]和[self manageObjectContext]。它們都可以在沒有任何警告的情況下編譯好,但會在運行時拋出錯誤 –

+0

它會拋出什麼錯誤? – Yuji

+0

[self managedObjectContext]與上面相同,即 - [_NSStateMarker managedObjectContext]:發送到實例0x100118290的無法識別的選擇器。 [[NSApp委託] managedObjectContext]也一樣。 –

相關問題