gpt4 book ai didi

swift - 调用 insertNewObjectForEntityForName(_ :, inManagedObjectContext: ) 时使用未声明的类型

转载 作者:行者123 更新时间:2023-11-30 13:24:26 25 4
gpt4 key购买 nike

我正在使用 objC 的核心数据书中的代码,但在尝试使用以下代码(NSManagedObjectContext 的扩展)将新对象插入托管对象上下文时遇到了一个奇怪的错误:

/**
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
}

插入失败并且对象未实例化。检查托管对象上下文(在本例中为 self)会提供以下调试信息:

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)

有什么想法吗?我是核心数据新手,所以我一无所知!

最佳答案

好的 - 找到了解决方案。正在创建一个对象,但无法将其转换为 NSManagedObject 子类,因为我未能在 Xcode 的数据模型定义中为实体定义正确设置类和类模块。我不知道为什么我收到如此神秘的错误消息,但是它让我想起了使用 Swift 1.0...!

Xcode core data model pane

关于swift - 调用 insertNewObjectForEntityForName(_ :, inManagedObjectContext: ) 时使用未声明的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com