CoreData - 不能将空字符串设置为属性的默认值 [英] CoreData - can't set empty string as default value for attribute

查看:23
本文介绍了CoreData - 不能将空字符串设置为属性的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据模型中有一个实体,其字符串属性当前是可选的,我想将其转换为默认值为空字符串的必需属性.

I have an entity in my datamodel with a string attribute that is currently optional, and I'd like to convert this to a required attribute with a default value of the empty string.

正如其他人所发现的,将 Xcode CoreData 数据建模器中的默认值留空会导致验证错误(因为设计人员将其解释为 NULL),但尝试将 ''、"" 或 @"" 作为默认值会导致在那些被解释为默认值的文字字符中,而不是根据需要的空零长度字符串.

As others have discovered, leaving the default value blank in the Xcode CoreData data modeller results in validation errors (since the designer interprets this as NULL), but trying '', "", or @"" as the default value results in those literal characters being interpreted as the default, rather than the empty zero-length string, as desired.

我确实找到了 this thread on Google,但是,除了解决方案非常丑陋(模型定义在 .xcdatamodel 和 objc 源之间拆分)之外,它也不适用于轻量级迁移,因为这些迁移仅基于 .xcdatamodel 文件完成,并且不会加载实体实现中的 objc 逻辑.

I did find this thread on Google, however, apart from the solution being really ugly (model definition split between the .xcdatamodel and objc source), it also doesn't work for lightweight migrations because those migrations are done solely based on the .xcdatamodel files and the objc logic from your entity implementations isn't loaded.

有没有办法在数据模型设计器中实现这一点?

Is there any way to achieve this in the data model designer?

推荐答案

这是一个非常有趣的问题.经过一些测试,我认为这是不可能的,因为数据模型中的文本字段的配置方式.

This is a very interesting question. After some testing I don't think this is possible because of the way the text field in the data model is configured.

原则上,您可以使用 u2205 的 unicode 空集字符来表示默认的空字符串,但文本字段似乎不接受任何转义,因此它会将任何尝试转义unicode 字符代码到代码字符本身的文字字符串,例如输入 'u2205' 以文字文本 'u2205' 结束.

In principle, you could use the unicode empty-set character of u2205 to represent a default empty string but the text field does not seem to accept any escapes so it converts any attempt to escape a unicode character code to the literal string of the code characters themselves e.g. entering 'u2205' ends up as the literal text 'u2205'.

理论上,您可以编写一个实用程序来读取以图形方式生成的托管对象模型文件,然后以编程方式将属性默认值设置为空字符串,然后将文件保存回磁盘.我说理论上"是因为没有记录的方法可以从代码中保存托管对象模型文件.您可以读取一个并在内存中对其进行修改,但不能保留更改.

In theory you could write a utility app to read in the graphically generated managed object model file and then programmatically set the attribute default to equal an empty string and then save the file back to disk. I say "in theory" because there is no documented way to way to save a managed object model file from code. You can read one and modify it in memory but not persist the changes.

我认为有点疏忽.

我认为您别无选择,只能在模型首次加载时实用地设置默认空字符串.这很简单,但很丑陋,你必须记住你做过(特别是如果你迁移版本),但我认为现在这是唯一的选择.

I don't think you have any choice but to set the default empty string pragmatically when the model first loads. That is simple to do but it's ugly and you'll have to remember you did (especially if you migrate versions) but I think right now that is the only choice.

这篇关于CoreData - 不能将空字符串设置为属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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