Erik’s Brain

The reward of a thing well done...

Xcode: Adding Core Data to Existing Project

This was a head scratcher.

I’m adding Core Data to an existing project that’s currently storing its data with Keyed Archives. So, step one of doing that is simply cloning my existing project so I can start reworking the plumbing, so to speak, without worrying about screwing things up horribly (because I most certainly will).

Project cloned. Now let’s work with the app delegate to add the Core Data stack and related methods. Lots of cutting and pasting from a Core Data-based template follows.

Done.

But wait, I also need to add the CoreData.framework and a line to the Prefix.pch file that imports the Core Data framework to every file in the project.

Compile errors up the wazoo. This is surely a sign that there is one glaring error someplace and once I fix that everything else will fall into place. But what is that one error? I’ve linked the framework to the target. I’ve imported the Core Data framework in all files. So why is the compiler refusing to recognize the NSManagedObjectContext property that I’m declaring in the AppDelegate header? It’s not even getting syntax highlighting applied to it. It’s as if the project doesn’t know what an NSManagedObjectContext (or NSManagedObjectModel or NSPersistentStoreCoordinator) is. As if it doesn’t know what the hell Core Data is.

Hmm…

I’ll cut to the chase. For whatever reason, in this Xcode project, the “location” for my Prefix.pch file was set to “Relative to SDK” instead of something reasonable like “Relative to Project.” So what does that mean? When I edited the Prefix.pch file, adding the #import <CoreData/CoreData.h> line, I was actually adding that to the original Prefix.pch that I had cloned this project from. Grrr…

Watch out for that. This has something to do with the fact that I originally created this Xcode project with Xcode 3.2.X and then moved over to Xcode 4. So chalk it up to really annoying growing pains that’ll make you want to throw your laptop out of a window. Okay, it wasn’t that bad. At least I wasn’t the idiot in this case. Xcode was the idiot.

I just hope every step along this “migrating to Core Data” path doesn’t go this poorly.

Comments