This article will not serve as an introduction to MOGenerator or it’s abilities – that subject has been covered elsewhere. The sole purpose of this guide is to provide you with the steps necessary to get MOGenerator pseudo-integrated with XCode 4, at least as much as possible at this time.
To summarize, we’ll be adding custom build steps to your project which instruct XCode to run shell scripts to process Core Data Model files. These scripts will determine the correct model version, call mogenerator to generate/update the entity classes, and then call momc to compile the model (this is usually performed by xcode, but since we’re inserting a new build rule for model files we need to handle it). I have tested these instructions on several different iOS projects, with multiple models and model versions.
Step 0) Download and install MOGenerator if you haven’t already. I used MOGenerator ver 1.22.
Step 1) Download the MOGenerator scripts and copy into a location in your project. For the purposes of this tutorial, I’ll put them in a folder named ‘scripts’ at the root of the project directory.
- REQUIRED: Modify the script to suits your needs. You will need to change the name of the ManagedObject class your entities should inherit from, and you may want to change the output directory. For an explanation of each parameter please refer to the MOGenerator documentation.
Step 2) On the “Build Rules” tab of your target in XCode, add two build rules; one for “Data model files” and another for “Data model version files”
- a. Change the Process dropdown to “Data Model Files”
- b. Change the Using dropdown to “Custom Script”
Step 3) Put the path to the scripts in your project. In this example, they are in a folder “scripts” in the same directory as the Project file (Here is a list of all XCode environment variables).
Step 4) Add the output file pattern so XCode is knows what this custom step produces.
Step 5) Repeat steps 2-4 for the Data Model Version files. Take note that the file type processed, script executed, and output from this step are different. Refer to the screenshot
Step 6) Build your project.
Step 7) Any entities which are new (that is, which MOGenerator has not encountered in a previous build) will need to be manually added to your project. This step only needs to be performed when you have new entities, not during updates to existing entities.
If you have multiple targets in your project which utilize your data model, you’ll need to add these build steps for each target to make sure the model is kept up to date for each target.
I’d like to thank Jean-Denis Muys for providing the XCode4 integration steps needed to make this possible, and Jonathan Rentzsch for creating mogenerator.