2014-06-22 98 views
3

我有一個相當奇怪的情況,在使用Swift進行實驗時發生核心數據錯誤。我不確定它是否來自Swift(測試版錯誤?)或者它是否是我。不過,這裏是我的測試用例的設置(在VTModelTests.swift中)。insertNewObjectForEntityForName的奇怪行爲導致NSInternalInconsistencyException

var bundle = NSBundle(forClass: VTModelTests.self) 
var url = bundle.URLForResource("VTDocument", withExtension:"momd") 
appleModel = NSManagedObjectModel(contentsOfURL: url) 
assert (appleModel != nil) 
var coord = NSPersistentStoreCoordinator(managedObjectModel: appleModel); 
var store = coord.addPersistentStoreWithType(NSInMemoryStoreType,configuration:nil,URL:nil,options:nil,error:nil); 
assert (store != nil) 
ctx = NSManagedObjectContext(); 
ctx!.persistentStoreCoordinator=coord 
ctx!.retainsRegisteredObjects=true; 

var drwName = "Drawing" 
var descs = ctx!.persistentStoreCoordinator.managedObjectModel.entitiesByName 
for e : AnyObject in descs.allKeys{ 
    assert (descs.objectForKey(e).name == e as String) 
    if (e as String == drwName) { 
     NSLog("yeah") 
    } 
} 
model = NSEntityDescription.insertNewObjectForEntityForName(drwName,inManagedObjectContext: ctx) as Drawing 

我的錯誤信息是這樣的:

2014-06-22 22:12:25.584 xctest[63792:303] yeah 
<unknown>:0: error: -[_TtC12VTModelTests12BaseTypeTest testTreeStructure] : failed: caught "NSInternalInconsistencyException", "+entityForName: could not locate an entity named 'Drawing' in this model." 

總之,我可以「證明」該實體名稱是存在的(即「耶」在日誌),但核心數據顯示名稱不在模型中的問題。循環的先前版本打印出實體,看起來不錯。我沒有任何第二個版本,並且在打印所有實體時,模型數據中正確顯示了一個新的更改名稱('Model'現在稱爲'Drawing')。編譯後的模型位於結果包中。

有人可以解釋一下嗎?還是必須等待Xcode 6的下一個beta版?我還忽略了什麼?提前謝謝了!

回答

6

我可以證實這個問題。不是一個答案,但我使用的方式是將insertNewObjectForEntityForName分割爲:

let entity = NSEntityDescription.entityForName("Photo", inManagedObjectContext:context) 
let photo = Photo(entity:entity, insertIntoManagedObjectContext:context) 
相關問題