简体   繁体   English

CoreData - 将实体的属性设置为非null - 属性是否应设置为Optional或Mandatory

[英]CoreData - Setting a property of entity to be not null - Should the attribute be set as Optional or Mandatory

I need to have an attribute in Coredata's entity be set as not null and have a default value. 我需要在Coredata的实体中将属性设置为非null并具有默认值。

I have set a default value for the attribute of the Entity in the .xcdatamodeld schema definition 我在.xcdatamodeld模式定义中为Entity的属性设置了默认值

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 ? 如果我不检查属性中的以下三个属性会产生什么影响 - Transient,Optional,Indexed? Will this attribute be treated as mandatory ? 这个属性会被视为强制性的吗?

EDIT: 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. 编辑:当我将属性设置为必需属性并且如果尝试设置的值为nil时,插入失败,错误代码为1570 - NSValidationMissingMandatoryPropertyError,其中我只是期望将属性设置为其默认值并插入。

What is the point of having a default value then ? 那么拥有默认值有什么意义呢? Can anyone help me what I am really missing here. 任何人都可以帮助我,我真的在这里失踪。

Set it as mandatory and configure it with an appropriate default value (eg 0 ). 将其设置为必需,并使用适当的默认值(例如0 )对其进行配置。 If it is optional, the setting of the default value is not guaranteed, regardless of the other options. 如果是可选项,则无论其他选项如何,都无法保证默认值的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM