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

查看:412
本文介绍了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.

I确实找到了这个线程,但是,除了解决方案真的丑陋(模型定义拆分.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.

原则上,您可以使用 \\\∅ 的unicode空集字符表示默认的空字符串但是文本字段似乎不接受任何转义,所以它转换任何尝试将unicode字符代码转换为代码字符本身的文字字符串例如输入'\\\∅'结尾为文字文本'\\\∅'。

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.

一个监督的位,我想。

Bit of an oversight, I think.

我不认为你有什么选择,但是在模型第一次加载时以pragmatically设置默认的空字符串。这很简单,但它是丑陋的,你必须记住你做了(特别是如果你迁移版本),但我认为,现在是唯一的选择。

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天全站免登陆