Core Data is one of the frameworks I find a bit scary to use when I’m developing iOS applications. I always felt like I was developing something that wasn’t really under my control. That was until Piet pointed out this framework to me: Mogenerator.
Draw your models
The first thing you should do is create your models in the XCode Modelling Tool. More information on this topic can be found here.
Here is an example of my Core Data model:
When you created a model with some attributes, you can use mogenerator to generate your classes (or regenerate them, when you did some editing).
I pass two arguments to mogenerator to generate the files.
- -m the location of your data model
- -O the location of your model classes
Here is an example:
This command generates all the classes for your models created with the modelling tool. Be aware: You still have to add them to your XCode project in orde to use them.
What is generated?
For each model four class files are generated. The 2 class files starting with a dash should never be modified. When you want to add custom methods/logix, you can do this in the other 2 generated classes. They extend from the dashed-classes.
Every attribute can now be accessed as a property on the object. This makes it very easy to assign new values.
Don’t forget that when you change your model in the modelling tool, that you’ll have to run the mogenerator command again in orde to regenerate the dashed files.
The code snippet below will create a Person object and assigns some values to the attributes. When everything is assigned the changes are saved.
Check these out!
Here are some links that helped me during development:
Sign up for our newsletter
Who’s got the mic?
Architecture & Design
Koen Van Der Auwera
Bob Van Landuyt
Nathan de Witte