1. 程式人生 > >Core Data遇到的問題(Unresolved error Error Domain=NSCocoaErrorDomain)

Core Data遇到的問題(Unresolved error Error Domain=NSCocoaErrorDomain)

Mac Version:10.6.7

XCode Version:4.2

參考書籍:Iphone3開發基礎教程中文版第十一章關於資料儲存技術------Core Data

在- (void)viewDidLoad  函式中

有如下的程式碼:

NSManagedObjectContext *context = [appDelegate managedObjectContext];

執行到這裡就出錯,錯誤提示資訊:
2011-12-23 11:35:25.865 Core Data Persistence[1115:207] Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x6a3e420 {metadata=<CFBasicHash 0x6a275b0 [0x171bb38]>{type = immutable dict, count = 7,
entries =>
...............

根據以下內容:

http://stackoverflow.com/questions/5517129/core-data-cocoa-error-134100

進行如下的操作就能解決問題:

找到該應用程式沙箱的Documents資料夾,刪除CoreData需要的那個檔案。

用以下的方法可以找到沙箱的Documents資料夾:

- (NSString *) dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString

*documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:@"test.txt"];

}


產生這種錯誤的步驟:

1.編寫xcdatamodeld檔案的時候,新增一個ENTITIES(命名為entitie)

2.給entitie添加了2個屬性a和b

3.執行程式,不會在NSManagedObjectContext *context = [appDelegate managedObjectContext];這裡出現錯誤,但是有很大的可能在其他地方出錯

4.重新命名entitie為Line

5.執行程式,這個時候就出現錯誤

最後總結該錯誤的原因:

The reason for this is because your new managed object model is trying to use older version of storage (the one first time created when you launched the app).

摘自:http://stackoverflow.com/questions/5517129/core-data-cocoa-error-134100