Swift - 在应用组中共享核心数据模型(w / extension) [英] Swift - Sharing Core Data Model in App Group (w/ extension)

查看:287
本文介绍了Swift - 在应用组中共享核心数据模型(w / extension)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用程序中使用相同的核心数据模型以及扩展,但不确定如何在2之间共享核心数据模型。核心数据模型的类使用名称空间类名,所以当我尝试和获取扩展中的对象时,我无法加载类名为的错误。


CoreData:warning:无法为实体
加载名为'Dali.Alarm'的类'Alarm'。找不到类,使用默认的NSManagedObject。


有没有在类名中使用命名空间,使扩展继承主项目的命名空间?



解决方案

看起来像一个真正的问题,它不会伤害作为一个功能请求/ bug与苹果。



在此期间,您可以通过两个步骤实现。首先,用 @objc(ClassName)标记你的NSManagedObject子类。只需将它插入类声明之上:

  @objc(ClassName)
className:NSManagedObjectSubclass {
@NSManaged var name:String
}

其次,回到托管对象模型,并从您正在使用的实体的检查器中的类名称字段中删除命名空间。



这段代码在我看来是这样的:


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: 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?

解决方案

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.

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.

This worked for me today, after reading this: I can't use my core data model in two targets in a Swift project

这篇关于Swift - 在应用组中共享核心数据模型(w / extension)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆