Integrating mogenerator in your XCode project

Every iOS app you build would require some sort of storage functionality within the device itself, especially if it caches data fetched from the network. Core Data, an object graph management framework developed by Apple will mostly likely be your choice in this regard. In that case, a tool like mogenerator is indispensable. mogenerator is a simple script that generates the model classes from the Core Data model (.xcdatamodel) defined in a project. Not just that, mogenerator also provides a few helper functions for your classes to make it simpler to use them for saving and fetching them. In this article, I’ll go through the necessary steps to add the mogenerator script to your project and automatically generate the necessary model classes whenever XCode builds your project.

Let’s begin by installing mogenerator on OS X using homebrew:

brew install mogenerator

Once installed, open your project in XCode and select the ‘Edit Scheme’ option under the Product menu:

Product
Menu

Under the Build Phase, select Pre-actions, tap ‘+’ at the bottom left and select ‘New Run Script Action’.

Build Phase
Pre-action

Configure the new run script as follows:
- Shell: /bin/sh
- Provide build settings for: \
- Script:

    cd ${SOURCE_ROOT}/${PROJECT_NAME}


    /usr/local/bin/mogenerator --template-var arc=true 
    -m ${PROJECT_NAME}.xcdatamodeld/${PROJECT_NAME}.xcdatamodel 
    -M Model/CoreData/Machine -H Model/CoreData/Human

This is how your script action should look like:

New Run Script
action

Click Ok  and you’re good to go! The next time you build your project, mogenerator will work it’s magic on your data models!

Go Top
comments powered by Disqus