2016-05-23 72 views
0

我正在玩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) 

任何想法?我是一名核心數據新手,所以我更無能爲力!

+0

您是否添加了'import coreData'? – vadian

+0

是的 - 核心數據存在和正確。我沒有收到任何編譯器錯誤,這表明庫至少在編譯時存在並且是正確的。 – rustproofFish

回答

0

確定 - 找到解決方案。一個對象正在被創建,但它不能被轉換到NSManagedObject子類,因爲我無法在Xcode的數據模型定義中正確設置實體定義的類和類模塊。我不知道爲什麼我得到這樣一個神祕的錯誤消息,但它讓我想起了使用Swift 1.0 ...!

Xcode core data model pane

+0

你如何解決這個問題?我也會得到相同的錯誤 – user2526811

+0

我會仔細檢查在您的數據模型中正確設置的東西(請參閱上文),因爲該區域中的錯誤導致我無法解決問題。這個問題似乎是因爲Swift嚴格遵守命名空間。 – rustproofFish

相關問題