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.
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 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?
Maybe I'm late with this answer, but I was Googling and found this forum.
The solution is very simple:
All changes are easy.
Part 2
Part 3
Whip out your favorite XML editor (I just used Emacs) and dive down to the
contents
file inside the.xcdatamodel
bundle inside the.xcdatamodeld
bundle. Then just add adefaultValueString=""
XML attribute to the<attribute>...</attribute>
element inside the<entity>...</entity>
brackets. Here's an example:I can't speak to whether this survives migration since I haven't had to do that yet.