CoreData - 将实体的属性设置为非null - 属性应设置为Optional或Mandatory [英] CoreData - Setting a property of entity to be not null - Should the attribute be set as Optional or Mandatory
问题描述
我需要将Coredata的实体中的属性设置为不为null并具有默认值。
I need to have an attribute in Coredata's entity be set as not null and have a default value.
我已为实体的属性设置了默认值在.xcdatamodeld模式定义
I have set a default value for the attribute of the Entity in the .xcdatamodeld schema definition
我的问题是属性是否被标记为可选。
My question is Should the attribute be marked as Optional or not.
将会影响如果我不检查以下三个属性的任何一个 - 瞬态,可选,索引?
What will be the impact if I don't check any of the following three to the attribute - Transient, Optional, Indexed ? Will this attribute be treated as mandatory ?
编辑:当我将属性设置为强制性,如果尝试设置的值为nil,则插入失败,错误代码为1570 - NSValidationMissingMandatoryPropertyError,其中我只是期望属性设置其默认值并插入。
When I set the property as mandatory and if the value trying to set is nil, the insert fails with error code 1570 - NSValidationMissingMandatoryPropertyError, where I am simply expecting the property to be set its default value and gets inserted.
具有默认值的意义是什么?任何人都可以帮助我这里真正缺少的。
What is the point of having a default value then ? Can anyone help me what I am really missing here.
推荐答案
设置为强制性并配置一个适当的默认值 0
)。如果是可选的,则不保证默认值的设置,而不考虑其他选项。
Set it as mandatory and configure it with an appropriate default value (e.g. 0
). If it is optional, the setting of the default value is not guaranteed, regardless of the other options.
这篇关于CoreData - 将实体的属性设置为非null - 属性应设置为Optional或Mandatory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!