Saving Time and Energy with Cocoa Programming Tips
Programming computers can be tedious and grueling. Save some time and make your Cocoa programming easier with these handy tips and tricks.
Use keyboard shortcuts
One of the easiest ways to speed up your Cocoa programming is to take advantage of the multitude of keyboard shortcuts available to you in Project Builder and Interface Builder. Table 1 lists some of the common keyboard shortcuts that Project Builder and Interface Builder share.
Table 1: Keyboard Shortcuts Shared by Project Builder and Interface Builder
What It Does
Saves a file
Opens a file
Minimizes a window to the Dock
Displays help for Project Builder or Interface Builder
Creates a new file in Project Builder or a new nib file in Interface Builder
Quits the application
Runs a project or an interface
Besides the run-of-the-mill keyboard shortcuts, you can help your Cocoa programming along by using the keyboard shortcuts of the pros. Table 2 lists some keyboard shortcuts that will give a decided advantage.
Table2: Keyboard Shortcuts Used by the Pros
What It Does
In Project Builder, looks up the keyword's definition in the Cocoa documentation
Goes back in time whenever you make a mistake. Project Builder and Interface Builder offer multiple Undos.
Finds all instances of your search term in the Cocoa documentation.
Control-click (or right-click if you have a multibutton mouse) anywhere in Project Builder or Interface Builder to reveal a large menu of context-sensitive functions.
Read the documentation
Your best bet for accelerating your Cocoa programming is to read the manual. Some documentation explains how the Developer Tools work; other documentation details the various classes and methods of Cocoa. You can view the built-in documentation by choosing Help --> Cocoa Help. You can also read the documentation by visiting Apple's Developer Web site:
Use Class Browser
In addition to using the standard documentation that's part of Project Builder, you can browse the various classes in Cocoa using Class Browser. Click the vertical Classes tab in Project Builder to display Class Browser. The Browser displays all the classes in Cocoa in a structured fashion. This gives you instant access to the definitions of the accessor functions of Cocoa classes.
Adjust the window count
Setting up your work environment to best suit your needs is another quick way to improve your Cocoa programming experience. Project Builder can operate with a different numbers of windows, and it's up to you to set the number of windows with which you feel most comfortable.
Some people use the single-window setting in the Task Templates section of the Preferences window. Other people don't appreciate having all the various components of Project Builder in one window and like to stretch out a bit. For them, Project builder offers a few other settings that force various Project Builder functions to appear in different windows.
Choose Project Builder --> Preferences and click the Task Templates button in the Toolbar to adjust the window count setting.
Use drag and drop
Drag and drop has long been an attractive feature of the Macintosh operating system. Project Builder and Interface Builder continue in this tradition, offering many different drag-and-drop features.
You can add files to Project Builder from Finder by dragging them into your project window. These files include source code files, frameworks, image files, HTML documents, rich text documents, and .icns icon files.
When you create classes in Project Builder, you can let your nib file know about them by dragging the header files for those classes from Project Builder to the Interface Builder nib file window. In Interface Builder, drag and drop is, perhaps, the most important interface operation. You use drag and drop to create the entire interface.
Initialize and awaken!
When your application loads its nib file, Cocoa creates the objects in that file and calls the init method. Next, your application sets the outlets for your interface and calls the awakeFromNib method. Because the application sets the outlets after the init method, you can't use any methods that rely on outlets. Because awakeFromNib loads after the outlets are set, you can use code that replies on outlets.
As a basic rule, try to add initialization code to the init method of your object. If it doesn't work properly or yields a compiler error, move that initialization code to the awakeFromNib method for that object.
Don't forget the superclass
One of the great features of object-oriented programming is the fact that it has a hierarchical structure. This has a wonderful benefit called inheritance. Because every class in Cocoa (with the exception of NSObject) is a subclass of some other class (or classes) above it in the class hierarchy, every class has more methods than those listed for its particular type. This can be confusing at first for some beginners.
Consider this example. The NSTextField and NSTextView controls in Cocoa seem like they should work in a similar fashion because they both display text. By taking a quick look at the documentation, however, you find that they are very different controls. The NSTextView control doesn't have a method for setting its text. In the documentation for NSTextView, you'll notice that it is a subclass of NSText. NSText, on the other hand, does have a method for setting the text of the view: setString. You may be tempted to use the setString method with an NSTextField control. It's a good guess but an incorrect one.
Like the NSTextView, the NSTextField control has no direct method for setting its text. Because it's a subclass of the NSControl class, you can use the setStringValue method from that class. The lesson here is that if you expect a control to have some kind of method and it doesn't, check one of the superclasses of that class to see whether one of them has the function you need.
Position items with Cocoa coordinates
Programmers who are migrating to Cocoa from other languages may be surprised to discover that Cocoa bases all its measurements on a different coordinate system. Whereas most computer languages define the upper-left corner of a view as the origin, Cocoa designates the bottom-left corner as the origin. You may remember this arrangement as Quadrant I from your high school trigonometry class.
If you're familiar with PDF and PostScript, you can skip this tip. You're already living in an upside-down world. PDF and PostScript use the same coordinate system that Mac OS X uses — and this is no accident. NeXT based its image model on Display Postscript. Mac OS X continued with this tradition by basing its image model on PDF, which is essentially PostScript in a document model.
Use guidelines in Interface Builder
When it comes to building interfaces, Apple insists that you follow many guidelines to ensure the best Aqua experience. Frankly, there are too many guidelines to keep them all straight. To help you out, Interface Builder offers a great feature in the form of Guidelines. As you drag controls around your interface, Interface Builder displays lines in the interface to help you align controls and place them according to the Aqua Human Interface guidelines.