Fixing the Xcode Project Templates

I recently upgraded my copy of Xcode and installed the iPhone SDK. A short while later, while following an example in the Pragmatic Programmer's Cocoa Programming book, I found that my copy of Xcode wasn't creating some key files.

Specifically, when I created a new project from the Cocoa Application template, the app delegate files (ProjectNameAppDelegate.*) weren't being created. Yet there they were in the book, which has been freshly updated for Snow Leopard and the latest version of Xcode (both of which I have). Hmmm.

I did some searching online, and found that I'm not alone. Others on the Pragmatic forums have had the same trouble, and Martin Wood has written a useful post explaining that the Xcode project template files were messed up. Having read Martin's post I was curious about what had gone wrong, and dug a little deeper.

Xcode stores template files for creating new projects in this folder:

/Developer/Library/Xcode/Project Templates

The Cocoa Application template (the one that I found to be dodgy) belongs here:

/Developer/Library/Xcode/Project Templates/Application/Cocoa Application

Inside it you should find the following files and directories:

English.lproj/
main.m
___PROJECTNAMEASIDENTIFIER___-Info.plist
___PROJECTNAMEASIDENTIFIER___AppDelegate.h
___PROJECTNAMEASIDENTIFIER___AppDelegate.m
___PROJECTNAMEASIDENTIFIER____Prefix.pch
___PROJECTNAME___.xcodeproj/

I found something rather different; I had several template directories inside my Cocoa Application folder, that should have been in the parent directory:

Cocoa Application/Cocoa Application
Cocoa Application/Cocoa Document-based Application
Cocoa Application/Core Data Application
Cocoa Application/Core Data Application with Spotlight Importer
Cocoa Application/Core Data Document-based Application
Cocoa Application/Core Data Document-based Application with Spotlight Importer

Interestingly, all these template folders (except for Core Data Application with Spotlight Importer) already existed inside Project Templates/Application.

The folders in Project Templates/Application dated from 2007 and used the outdated binary Nib files (with a .nib file extension). The templates in inside Cocoa Application dated from May 2009 and used the more up to date .xib file format. It seems fairly clear that those are the ones we should be using.

I ran the following commands to move the older copies out of the way:

$ mkdir ~/xcode-backup
$ cd "/Developer/Library/Xcode/Project Templates/Application"
$ for template in \
>   $(find Cocoa\ Application -type d -mindepth 1 -maxdepth 1 | egrep '(Core |Cocoa D)'); do
>     old="$(basename $template)"
>     [ -e $old ] && mv "$old" ~/xcode-backup
>     mv "$template" .
>   done
$ mv "Cocoa Application" ~/xcode-backup
$ mv "~/xcode-backup/Cocoa Application/Cocoa Application" .

I restarted Xcode and made myself a new project and found that the delegate files were present and correct. I also noticed that the Cocoa Application project (which had previously had the standard blue project icon) now stood out with a separate icon:

I love feedback and questions -- please say hello on Twitter or leave a comment.

Published on in Mac OS X