问题描述
我想在程序运行期间以编程方式将属性添加到实体。这是你推荐做的,还是会导致问题?
我需要如何组合 NSAttributeDescription
和 NSEntityDescription
?我熟悉使用Xcode创建模型,但是没有使用 NSEntityDescription
。
这在理论上是可能的,但并不实用。
您可以修改 NSManagedObjectModel
以编程方式,以及 NSEntityDescription
。注意,如果修改模型,则 -setEntities:
(NSManagedObjectModel)和 -setProperties:
(NSEntityDescription)已实例化。因此,您不能修改现有模型的结构。您必须创建一个新的,并将所有的数据从旧的核心数据堆栈复制到新的基于您的新模型。
使用NSMutableDictionary是一种更清醒的方法。
I would like to add attributes programmatically to an entity during runtime of my app.
Is this something you would recommend doing or can this lead to issues?
How would I need to combine NSAttributeDescription
and NSEntityDescription
? I am familiar with creating models using Xcode, but did not do it using NSEntityDescription
yet.
It's theoretically possible, but doesn't appear very practical.
You can modify the NSManagedObjectModel
programmatically, as well as NSEntityDescription
. Note that -setEntities:
(NSManagedObjectModel) and -setProperties:
(NSEntityDescription) both trigger exceptions if you modify a model that has been instantiated. So you can't modify your existing model's structure. You'd have to create a new one and copy all of your data from the old Core Data stack to the new one based on your new model..
Using NSMutableDictionary is a much saner approach.
这篇关于如何以编程方式向现有Core Data实体(对象)添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!