by Alex Rozanski

Xcoding made easy

Jun 25th 2009Published in Development, Tools

Xcode is Apple’s Integrated Development Environment for applications written for OS X or iPhone OS. With the increasing popularity for software written for both platforms, here are some useful features of Xcode that make coding using it a much smoother and easier process.

Keyboard Shortcuts

There are a plethora of keyboard shortcuts in Xcode to perform common tasks associated with various aspects of the development tool. Here is a handy chart of all the Xcode keyboard shortcuts available (note that this chart was created by Colin Wheeler):

All-In-One View

In the Xcode preferences, the default layout is, oddly enough, the Default layout. This is better set to All-In-One, since it adds extra panes to the main window, such as the Page toolbar item, which allows you to switch between the Project and Debug modes with one click, in the same main window. Also, a tabbed menu is added above the editor window, with tabs for Detail, Project Find, SCM Results and Build, which make it easier to access different windows within your project, keeping them within the same workspace. To change the layout to All-In-One, open up the Xcode preferences (⌘+,) and in the General preferences, change the Layout preference to All-In-One.

The #pragma mark directive

The #pragma mark directive is very useful in organising implementation files, especially those with a large number of methods. It can be placed anywhere in the source files, and is used as a way of grouping common methods. It takes the form:

#pragma mark <Label>

Where <Label> is the name you want to associate. For example:


#pragma mark Drawing- (void)drawRect:(NSRect)rect {
// Drawing code here.
}//... other methods here, if necessary

and it allows you to group together methods in the Project Symbols list; with any method signature following the group name being listed underneath the group title you have assigned.

Clicking on any of the method names selects the method declaration in the file and scrolls down to that point; in this way it is easy to navigate around the implementation files, even if they contain a lot of code or methods.

Finding APIs in the documentation

When writing code in Xcode, it is not uncommon for you to need to take a look at the documentation for a particular symbol, be that method, class, or even data types, for example. You could do this by going to Help > Documentation and then typing in the name of the symbol into the search field, and then hitting return.

However, there is an easier way. In your source file, double-click on the word that you want to look up in the docs, whilst holding down the ⌥ (option) key. This will then open up the documentation window, filling the search field in with the word that you want to look up.

Finding declarations

It may also be the case that you want to look up the declarations for symbols, as defined in the headers. For example, you may wish to find the interface for NSObject or the typedef for NSUInteger. Again, this is easily accomplished. In your source file, double-click on the symbol that you want to find the declaration for, whilst holding down the ⌘ (command) key.

Bookmarking Code

Xcode allows you to add bookmarks to your project, not just of source files themselves, but even lines within the files. These bookmarks get added to the Bookmarks section in the Groups & Files pane. To add a bookmark, place the text caret on the line which you want to bookmark, and select Edit > Add to Bookmarks or (⌘+D); the name of the file the line originates from, and the line number will be displayed as the bookmark name, although this can be renamed by clicking on the bookmark name.

Go to Counterpart

Classes are one of the major additions to the Objective-C superset of C, and are used frequently in. There exists class interface and implementation files for any class, and it is frequent to switch between both, such as when declaring a method in the interface, then implementing it in the implementation. There is a button which makes switching between the two easier, titled Go to Counterpart; clicking on it will change the editor window to the implementation for that class, if working within the class interface file, or the interface if working within the implementation file.

Whether the counterpart is opened in the same editor window or another editor window is determined in the Xcode General preferences, with the “Open counterparts in same editor” checkbox.

Simple Housekeeping

When dealing with Xcode projects, the number of files present can quickly mount up, whether that be source code, resources, NIBs or any other files that your project uses. When adding files to the project, Xcode doesn’t add them to any particular directory in the Groups & Files list, so it is up to you to do this. Note that the directory structure in the Groups & Files list is purely virtual – any movement of files within the hierarchy has no physical effect on disk. To add groups to the list, simply right click in the window and select Add > New Group, and once added, groups can be dragged around like you would in a Finder window. Here is a summary of the various groups and files within the project subdirectory:

  1. Classes: as it says on the tin, place class interface (.h) and implementation (.m) files in here. It may be useful to group class files based on their purpose, for example naming groups Controllers or Models, etc to make it easier to find the source files when you need them.
  2. Other sources: this group is for source files that do not declare classes; main.m is here by default. Since most source files deal with classes, there are usually not so many files in here, unless you are creating a non-AppKit based application, such as a Foundation Tool.
  3. Resources: where all the project resources are placed, be that NIBs, or images, for example, this is the place to keep it. It may be useful to group resources based on their function, especially NIBs since in larger projects the number of these can quickly mount up.
  4. Frameworks: this is where the frameworks that your project uses are. Xcode adds the Cocoa, AppKit and Foundation frameworks for you when a project is created, but if you need to link against others, this is the place to put them. If a lot of frameworks are being linked to, it would be best to create extra subdirectories to organise the linked frameworks. Frameworks can be added by right clicking in the Groups & Files window and selecting Add > Existing Frameworks..
  5. Products: these are the products of building the target(s). Normally there is only one product, your .app application file, so extra organisation of this subdirectory is not usually particularly necessary.

Debugging

A lot of time is often spent debugging your application. The interface can be made more straightforward in the Debugging pane of the Xcode preferences. There is a popup menu labelled On Start: this is how the workspace will change when debugging of your application begins. Its default is Do Nothing, however selecting Show Console, Show Debugger or Show Console and Debugger are useful so that when you begin to debug your application the workspace can update itself accordingly to make debugging a more straightforward process.

Start with Performance Tool..

The Xcode Tools do not encompass simply Xcode and Interface Builder; there are a few other other tools bundled with the package that are useful when testing projects, such as Instruments and Shark. Instead of having to build your application every time then manually loading it into the designated tool, it can be done from the Run menu. From Run > Start With Performance Tool, you can load your application into Shark, MallocDebug or one of the Instruments templates, such as Leaks.

Comments — 1

  1. Geoff Shaw

    Oct 7, 2009

    Have enjoyed your musings. You have a knack for describing relatively complex concepts and processes very clearly and unambiguously.

Add comment

Please enter your name and comment