Xcode 4.2: Application and Logic Unit Test Classes
In Xcode 4.2 when you add a new Objective-C test case class to your project and click the Next button, you will see a Test Type menu. This menu has two choices: Application and Logic. The menu determines the unit test Xcode adds in the class’s implementation file. If you choose Application, Xcode adds a test for the application delegate. If you choose Logic, Xcode adds a dummy test that asserts 1 + 1 = 2.
It doesn’t matter what you choose from the Test Type menu. You’ll probably delete the unit test Xcode creates and add your tests. You can add your tests to both test types.
Xcode 4: Open New Tab When Double-Clicking a File
Xcode 4 adds support for tabbed editing. When using tabbed editing you most likely want Xcode to open a new tab when you open a file from the project navigator. To get this behavior you must tell Xcode to open a new tab when you double-click a file in the project navigator.
Open Xcode’s preferences. Click the General button. Choose Uses Separate Tab from the Double Click Navigation menu.

Now when you double-click a file in the project navigator, Xcode opens the file in a new tab.
Xcode 4 Print Book Now Available
I am happy to announce that the print version of Xcode Tools Sensei is available. Go to the book’s site to buy it. The print version of the book will also be available on Amazon soon. I submitted the book to Amazon last night and was told it would take 5-7 business days for it to appear on Amazon. This means the book should be on Amazon late next week or the week after that. I will add a link to the book’s Amazon page when the book is available on Amazon.
Creating Custom Xcode 4 File Templates
This post contains the material on custom file templates I had prepared for the book. I decided not to put this material in the book because it wasn’t strong enough for me to support. But information on creating custom file templates in Xcode 4 isn’t easily available so I’m making it available here. I hope it helps you create your own file templates.
File Template Contents
At a minimum a file template has the following items:
- A folder with extension .xctemplate
- A property list file TemplateInfo.plist
- The file itself, which usually has the name ___FILEBASENAME___ (triple underscores)
File templates can also include additional files, such as xib files and icon files.
The .xctemplate extension defines the folder as a template folder. The name of the folder is the name that appears in the list of templates for the selected category in the New File Assistant. The name of the folder is the name of the template.
The extension for the ___FILEBASENAME___ file depends on the language. If you create a C, C++, or Objective-C template, there may be a second file for the header file.
Apple’s File Templates
The easiest way to create a custom file template is to copy one of Apple’s templates and modify it to suit your needs. Apple’s file templates also provide good examples that you can study to help you build your own templates. Apple’s iOS file templates are in the following location:
Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates
The rest of Apple’s file templates are in the following location:
Developer/Library/Xcode/Templates/File Templates
Developer is where you installed Xcode.
Making Changes to Existing Templates
After creating a copy of one of Apple’s templates, you must modify the files to customize them. Open the ___FILEBASENAME___ file and the TemplateInfo.plist file in Xcode to make your changes. I’m going to cover the keys in the TemplateInfo.plist file later in this post.
Moving the Template Folder
For Xcode to find your file template you must move the template folder to the user templates folder, which is in the following location:
/Users/Username/Library/Developer/Xcode/Templates/File Templates/GroupName
You may need to manually create some of the folders in the path to the user templates folder. GroupName is the name of the category on the left side of the New File Assistant. You can create your own group name or use one of the built-in names. Your file template will appear in the GroupName category.
TemplateInfo.plist Keys
The TemplateInfo.plist file has the following keys (this may not be an exhaustive list) for file templates:
- AllowedTypes
- CounterpartTemplateFile
- DefaultCompletionName
- Description
- Kind
- MainTemplateFile
- MacOSXVersionMin
- Options
- Platforms
- SortOrder
- Summary
Your template does not have to use all these keys. The data type for the keys is usually one of the following: Array, Boolean, Dictionary, or String.
AllowedTypes Key
The AllowedTypes key is an array that contains allowed file types. Source code file templates use this key. The following are examples of common allowed types:
- public.objective-c-source (Objective-C)
- public.objective-c-plus-plus-source (Objective C++)
- public.c-source (C)
- public.c-plus-plus.source (C++)
- public.c-header (header file for C, C++, Objective C)
- public.ruby-script (Ruby)
- public.python-script (Python)
- com.sun.java-class (Java .class file)
- com.sun.java-source (Java .java file)
- public.xml (XML)
- public.source-code (generic source code)
Use the public.source-code type for languages other than the ones I listed.
CounterpartTemplateFile Key
Only languages that have header files use the CounterpartTemplateFile key. Supply the name of the header file.
DefaultCompletionName Key
The DefaultCompletionName key contains the initial name for the file. When you save the file for the first time, this name appears as the file name in the Save panel.
Description Key
The Description key describes the file template. The contents of the Description key appear at the bottom of the New File Assistant when you select the file template.
Kind Key
Text files have the following Kind key value:
Xcode.IDEKit.TextSubstitutionFileTemplateKind
MainTemplateFile Key
The value of MainTemplateFile should be the name of the file in the .xctemplate folder. It generally has the following value:
___FILEBASENAME___.Extension (triple underscores)
Where Extension depends on the type of file you’re creating. For C, C++, and Objective-C files, the main template file is the implementation file, not the header file. The extension for an Objective-C file is .m.
MacOSXVersionMin Key
The MacOSXVersionMin key is the earliest version of Mac OS X that can use the template.
Options Key
The Options key is an array that lets you add controls to the New File Assistant. You can see an example of a custom control if you add an Objective-C class to a project. When you add an Objective-C class, there is a combo box that lets you choose the subclass. The combo box is an example of a custom control.
Create a dictionary key for each control you want to add. Add keys to the dictionary key. The following are common keys for controls:
- Type identifies the type of control. Pop-up menus should use a value of popup. Checkboxes should use a value of checkbox. Text fields should use a value of text. Combo boxes should use a value of combo.
- Name is the label for the control.
- Description contains tool tip text for the control.
- Identifier is a string that uniquely identifies the control.
- Default is a string that provides a default value for a control.
- Values is an array that contains the entries for a combo box or pop-up menu.
- Required is a Boolean key that says whether a condition has to be true for the control to be enabled.
Platforms Key
The Platforms key is an array of strings that identifies the platforms the template works on. A Mac file template has the following value:
com.apple.platform.macosx
An iOS file template has the following value:
com.apple.platform.iphoneos
SortOrder Key
I’m not exactly sure what the SortOrder key does. I haven’t seen a sort order value other than 1 in Apple’s templates.
Summary Key
The Summary key summarizes what the file template does. The summary is a shorter form of the file template’s description.
SDL OpenGL Typedef Redefinition Error on Mac OS X 10.7
If you build a SDL OpenGL application for Mac OS X using SDL 1.2.14 (using the binary installer at the SDL site) and the Mac OS X 10.7 SDK, you can get the following error:
typedef redefinition with different types ('unsigned int' vs 'void *')
The SDL header file SDL_opengl.h and the OpenGL header file glext.h both define a data type GLhandleARB. SDL_opengl.h defines it as an unsigned integer. The Mac OS X 10.7 version of glext.h defines it as a void pointer. Defining GLhandleARB as different data types in different header files causes problems.
The SDL team fixed the problem in their Mercurial repository. The fix is not too difficult to apply. In your SDL_opengl.h file, change the following line of code:
typedef unsigned int GLhandleARB;
To the following:
#if defined(__APPLE__)
typedef void *GLhandleARB;
#else
typedef unsigned int GLhandleARB;
#endif
The fix defines GLhandleARB as a void pointer on Mac OS X and an unsigned integer on other operating systems.
Xcode 4.2 Convert to ARC Troubleshooting
One of Xcode 4.2′s new features is compiler support for ARC (Automatic Reference Counting), which automates memory management of Objective-C objects. Xcode 4.2 includes a refactoring tool to convert an existing project to ARC. Choose Edit > Refactor > Convert to Objective-C ARC. When you convert a project to ARC, a sheet opens that walks you through the conversion. But there are a couple of problems that can occur.
Problem 1: Clicking the Save Button Does Nothing
In the last step of the conversion, Xcode shows the changes it will make to each file when it does the conversion. Clicking the Save button is supposed to complete the conversion, but you may find nothing happens when you click the Save button. The only way to close the sheet is to click the Cancel button.
You can get this problem if your project is in a git repository. Xcode is set to take a snapshot of your project when you convert it to ARC. But snapshots do not work well with git repositories. Xcode tries to take a snapshot, the snapshot fails, and Xcode stops the conversion.
The workaround is to move the .git folder out of your project folder temporarily, convert the project, and move the .git folder back. Files that start with a period are hidden by default in the Finder so you will either have to use the Terminal to move the .git folder or use a utility that shows hidden folders in the Finder.
Problem 2: Release Builds of Mac Projects Generate Build Errors
After converting your project to ARC, you may get the following error when you build your project:
-fobjc-arc is not supported with fragile abi
Xcode generates this error when you try to build a 32-bit version of a Mac application that uses ARC. ARC can build only 64-bit Mac applications. The Release build configuration is set to build both 32-bit and 64-bit Intel binaries. The error occurs when building with the Release build configuration because Xcode is trying to build a 32-bit version of your project, which ARC does not support. The solution is to set the Architectures build setting to 64-bit Intel.
Xcode Book Updated for Xcode 4.2
The electronic version of Xcode Tools Sensei has been updated for Xcode 4.2. Go to the book’s site to buy it. You can also read the table of contents, introduction, and a sample chapter at the book’s site.
You may be wondering when the print version will be available. The release of the print book is weeks away. I have to layout the print book, create the index, and have a test copy shipped to me to make sure there are no glaring errors before I can release the print version.