子类化 NSManagedObject 子类
Subclass an NSManagedObject subclass
假设我有一个 NSManagedObject 子类 Instrument
,我想继承该子类以创建类似 Guitar
的东西。有这方面的普遍做法吗?它似乎不像子类化 NSObject 那样简单。
这样做没有错:
class Instrument: NSManagedObject {
@NSManaged var name: String
}
class Guitar: Instrument {
@NSManaged var numberOfString: NSNumber
}
对于托管对象子classes,subclass/parent class 关系
对应于的subentity/parent实体关系
核心数据实体。
如果您将“吉他”的“父实体”设置为“乐器”
Core Data model inspector 然后创建托管对象 subclasses
在 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.
假设我有一个 NSManagedObject 子类 Instrument
,我想继承该子类以创建类似 Guitar
的东西。有这方面的普遍做法吗?它似乎不像子类化 NSObject 那样简单。
这样做没有错:
class Instrument: NSManagedObject {
@NSManaged var name: String
}
class Guitar: Instrument {
@NSManaged var numberOfString: NSNumber
}
对于托管对象子classes,subclass/parent class 关系 对应于的subentity/parent实体关系 核心数据实体。
如果您将“吉他”的“父实体”设置为“乐器” Core Data model inspector 然后创建托管对象 subclasses 在 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.