ios - 子类化 NSManagedObject 子类

标签 ios swift core-data nsmanagedobject

假设我有一个 NSManagedObject 子类 Instrument,我想继承该子类以创建类似 Guitar 的东西。有这方面的普遍做法吗?它似乎不像子类化 NSObject 那样简单。

最佳答案

对于托管对象子类,子类/父类关系 对应于子实体/父实体关系 核心数据实体。

如果您将“吉他”的“父实体”设置为“乐器” 核心数据模型检查器,然后创建托管对象子类 在 Xcode 中,你会得到

// Instrument.swift:
class Instrument: NSManagedObject {

// Insert code here to add functionality to your managed object subclass

}

// Guitar.swift:
class Guitar: Instrument {

// Insert code here to add functionality to your managed object subclass

}

有关详细信息,请参阅“实体继承”部分 在Core Data Programming Guide :

Entity inheritance works in a similar way to class inheritance, and is useful for the same reasons. If you have a number of entities that are similar, you can factor the common properties into a superentity, also known as a parent entity.

还要注意

NOTE

Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity will exist within the same table in SQLite. This factor in the design of the SQLite persistent store can create a performance issue.

关于ios - 子类化 NSManagedObject 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610371/

相关文章:

ios - 试图保存 NSManagedObjectContext 不工作

swift - 如何使用核心数据保存在写板下方(用于签名)

ios - RBG24 在 Windows 上可以很好地播放,但在 iOS 上却不行

ios - 获取上次关闭的iOS App的信息

ios - 将一次性电子邮件注册添加到我的应用程序的开头

ios - NSFetchRequest 获取具有下一个最接近字符串属性的对象

ios - 根据数组 iOS 中的值更改单元格的颜色

ios - SwiftUI TabView PageTabViewStyle 阻止更改选项卡?

objective-c - 初始化之前使用的变量注释

swift - 将 UIViewController 呈现为具有透明背景的模态