Describe the properties that compose an entity.
- Core Data
Once you create an entity as described in Configuring Entities, you can add attributes to that entity.
An attribute describes a property of an entity. At minimum, you need to specify the property’s name and data type, whether it should be saved in the store, and whether it is required to have a value when it’s saved.
For some attribute types you can also choose whether to use a scalar type to represent the attribute in generated classes, as well as configure the attribute to have a default value, or to apply data validation rules.
Add an attribute as indicated in the screenshot and the steps that follow.
With an entity selected, click Add Attribute at the bottom of the editor area. A new attribute with placeholder name
attribute, of type
Undefined, appears in the Attributes list.
In the Attributes list, double-click the newly added attribute, and name it in place.
In the Attributes list, as shown in the second screenshot, click on
Undefinedand select the attribute’s data type from the Type dropdown list.
Use the data model inspector (choose View > Inspectors > Show Data Model Inspector) to configure attributes.
Transient attributes aren’t saved to the persistent store. By default, attributes are saved to the store. Transient attributes are a useful place to temporarily store calculated or derived values. Core Data does track changes to transient property values for undo purposes.
Optional attributes aren’t required to have a value when saved to the persistent store. Attributes are optional by default.
Core Data optionals aren’t the same as Swift optionals. You can use a Swift optional to represent a required attribute, for example, if you need flexibility to set its value during the time between the object’s initialization and its first save.
- Attribute Type
The attribute’s data type. This field reflects the selection made in the Attributes list’s Type dropdown.
For the full list of types, see
- Default Value
Most types allow you to supply a default value. New object instances set the attribute to this default value on initialization, unless you specify another value at that time.
Supplying a default value, in combination with making the type non-optional, can provide performance benefits.
Optionally, set validation rules such as the minimum and maximum values for a numeric type, or regular expressions requirements for strings. The data model inspector shows validation options specific to the selected attribute’s type.
- Use Scalar Type
Optionally, for some types, choose between scalar and non-scalar representations during code generation. For a
Double, selecting the Use Scalar checkbox produces a
Double, while leaving it unselected produces an
For the full list of types, including scalar variants, see
- Index in Spotlight
Adds the field to the Spotlight index for instances created from this entity.
For more information, see Core Spotlight.
- Preserve After Deletion
Includes the attribute in this entity’s tombstone. When persistent history tracking is enabled and a managed object is deleted, an identifying marker, known as its tombstone, is recorded in the relevant transaction.
- User Info
A dictionary in which you can store any application-specific information related to the attribute.
- Versioning Hash Modifier
Provide a hash modifier when maintaining multiple model versions if the structure of an attribute is the same, but the format or content of its data has changed.
- Versioning Renaming ID
If you rename an attribute between model versions, set the renaming identifier in the new model to the name of the corresponding attribute in the previous model.
For more information, see Using Lightweight Migration.