以编程方式创建属性 - Core Data [英] Programmatically create attribute - Core Data

查看:115
本文介绍了以编程方式创建属性 - Core Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的iphone项目,它包含一个简单的xcdatamodel,有一个单一的实体
,大约有3个属性。

i have a simple iphone project which contains a simple xcdatamodel that has a single entity with roughly 3 attributes..

我想知道是一种以编程方式向一个实体添加属性的方法。
ie如果用户按下某个类型的添加按钮,一个简单的字符串属性被添加到实体并保存..

i want to know if there is a way to programmatically add an attribute to an entity.. i.e. if the user presses an "add" button of some kind, a simple string attribute is added to the entity and saved..

如果这不可能,有人可能指向我的方向正确。

If this is not possible could someone point me in the right direction..

推荐答案

可以通过编程方式更改实体,但是在分配给受管对象上下文之后无法更改受管对象模型,因此无法对任何用户定义的更改使用它。在任何情况下,你不想以编程方式添加实体,因为这将使你以前创建的持久存储文件无用。

You can programmatically alter entities but you can't alter a managed object model after it has been assigned to a managed object context so that makes it useless for any user defined alterations. In any case, you wouldn't want to add entities programmatically because that would make your previously created persistent store file useless.

如果您想创建更自由形式的用户可扩展数据模型,则必须通过向另一个实体添加可选关系来回退并使您的实体更灵活或可以建模附加数据的实体继承组。

If you want to create a more free-form , user extensible data model, you have to back out and make your entities more flexible by adding an optional relationship to another entity or entity inheritance group that can model additional data.

例如:假设您有联系人列表,并且您要向每个联系人添加自由表单字段。你可以这样设置你的实体。

For example: Suppose you have a contact list and you want to add free form fields to each contact. You would set up your entities like this.

Contact{
    name:string
    phone:string
    userDefinedFields<-->>UserDefined.contact
}

UserDefined{
    name:string
    contact<<-->Contact.userDefinedFields
}

每当用户添加一个新字段时,您将创建一个新的 UserDefined 对象并添加 Contact.userDefinedFeilds 关系。你可以根据需要肉身。如果您需要多种类型的用户定义字段,则应按如下设置:

Whenever the user adds a new field, you create a new UserDefined object and add it the Contact.userDefinedFeilds relationship. You can flesh that out as needed. If you need more than one type of user defined field you should set it up like this:

Contact{
    name:string
    phone:string
    userDefinedFields<-->>UserDefined.contact
}

UserDefined{
    name:string
    contact<<-->Contact.userDefinedFields
}

TextField:UserDefined{
    text:string
}

NumberField:UserDefined{
    numValue:Number
}

或NumberField对象插入到 Contact.userDefinedFields 中。

You can then drop in a TextField or NumberField object into the Contact.userDefinedFields as needed.

这篇关于以编程方式创建属性 - Core Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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