我正在尝试在我的应用程序和扩展程序中使用相同的核心数据模型,但不确定如何在两者之间共享核心数据模型.核心数据模型的类使用带有类名的命名空间,所以当我尝试获取扩展中的对象时,我得到了无法加载名为"的类的错误.
I'm trying to use the same core data model in my app as well as an extension, but am unsure how to share the core data model between the 2. The class for the core data model uses a namespace with the classname, so when I try and fetch the objects in the extension I get the unable to load class named "" error.
CoreData:警告:无法为实体加载名为Dali.Alarm"的类'警报'.找不到类,改用默认的 NSManagedObject.
CoreData: warning: Unable to load class named 'Dali.Alarm' for entity 'Alarm'. Class not found, using default NSManagedObject instead.
有没有办法在类名中不使用命名空间,或者有没有办法让扩展继承主项目的命名空间?
Is there anyway to not use a namespace in the classname, or is there a way to make the extension inherit the namespace of the main project?
我之前被卡住了.似乎是一个真正的问题,将其作为功能请求/错误提交给 Apple 并没有什么坏处.
I was stuck on this earlier. Seems like a real issue, and it wouldn't hurt to file it as a feature request/bug with Apple.
与此同时,您可以通过两个步骤绕过它.首先,用 @objc(ClassName)
标记你的 NSManagedObject 子类.只需将其插入类声明上方:
In the meantime, you can get around it with two steps. First, tag your NSManagedObject subclasses with @objc(ClassName)
. Simply insert it above the class declaration:
@objc(ClassName)
class ClassName: NSManagedObjectSubclass {
@NSManaged var name : String
}
其次,返回托管对象模型,并从您正在使用的实体的检查器中的类"名称字段中删除命名空间.
Second, go back to the managed object model, and remove the namespace from the "class" name field in the inspector for the Entity you're working with.
今天这对我有用,在阅读以下内容后:我无法在 Swift 项目的两个目标中使用我的核心数据模型
This worked for me today, after reading this: I can't use my core data model in two targets in a Swift project
这篇关于Swift - 在应用程序组中共享核心数据模型(带扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!