With dozens of models with dozens of properties each, all able to be saved to disk using the NSCoder
protocol and NSKeyed(Un)archiver
, it's a lot of work to create and maintain models for my iOS application. With a dozen of properties for one model, I have to:
define a dozen of properties (which is ok, because they HAVE to be defined somewhere)
fill the
initWithCoder:
method, that's a dozen lines and two dozen times typing/copy-pasting the property name (or a constant key), 3) some goes forencodeWithCoder:
.
The risk of mistakes (either in typing, copying or changing (or not changing) things like encodeObject:
to encodeInteger:
is huge. Besides, it's simply a lot of manual work, which I prefer not to do. After all, we have computers to do that for us.
First I looked into something like simplifying the work. Something like this:
#define encodeObject(x) [encoder encodeObject:[self valueForKey:x] forKey:x]
#define decodeObject(x) [self setValue:[decoder decodeObjectForKey:x] forKey:x]
But that is still a lot of work. Can't this be done any simpeler? I do not prefer to do this on runtime using introspection or something.
I am thinking about some Python script that generates/manipulates the model files and adds them to Xcode. This could be based upon some JSON description file.
Having Xcode run this automatically in the pre-build phase would be great. Meaning a single place where I define every model and every property once and never having to worry about mistakes and properties I forgot to encode/decode.
Is the anything like that out there? Are there methods to add files to an Xcode project, without messing with some unreadable Apple "XML" project files? And what about including the headers in the target.. Or maybe some other great method to save me all that work?