我正在玩objC的核心數據手冊中的代碼,但在試圖將新對象插入托管對象上下文時遇到了一個奇怪的錯誤以下代碼是NSManagedObjectContext上的擴展:在調用insertNewObjectForEntityForName(_:,inManagedObjectContext:)時使用未聲明的類型
/**
Helper method - avoids manual downcast result of an insert action and entity does not have to be referenced by name
- returns: new entity (NSManagedObject)
*/
public func insertObject<A: ManagedObject where A: ManagedObjectType>() -> A {
NSLog("\(A.entityName) - \(self)")
guard let obj = NSEntityDescription.insertNewObjectForEntityForName(A.entityName, inManagedObjectContext: self) as? A else
{ fatalError("Failed to insert entity into context") }
return obj
}
插入失敗並且未實例化對象。檢查被管理的對象上下文(在這種情況下是自己)給出以下調試信息:
Printing description of self:
expression produced error: /var/folders/__/c3n7c0bd35v5f7qxv11gcg280000gn/T/lldb/25862/expr10.swift:1:46: error: use of undeclared type 'CoreData'
$__lldb__DumpForDebugger(Swift.UnsafePointer<CoreData.NSManagedObjectContext>(bitPattern: 0x116b90fa0).memory)
^~~~~~~~
/var/folders/__/c3n7c0bd35v5f7qxv11gcg280000gn/T/lldb/25862/expr10.swift:1:45: note: while parsing this '<' as a type parameter bracket
$__lldb__DumpForDebugger(Swift.UnsafePointer<CoreData.NSManagedObjectContext>(bitPattern: 0x116b90fa0).memory)
任何想法?我是一名核心數據新手,所以我更無能爲力!
您是否添加了'import coreData'? – vadian
是的 - 核心數據存在和正確。我沒有收到任何編譯器錯誤,這表明庫至少在編譯時存在並且是正確的。 – rustproofFish