Programmatically create attribute - Core Data

前端 未结 2 1167
礼貌的吻别
礼貌的吻别 2021-01-07 09:41

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

i want to know if there is a way to programma

相关标签:
2条回答
  • 2021-01-07 10:23

    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
    }
    

    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
    }
    

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

    0 讨论(0)
  • 2021-01-07 10:27

    i am not so sure if you can add an attribute with code, but maybe you can consider using one to many relationship?

    0 讨论(0)
提交回复
热议问题