Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [20]

By Root 924 0
systems, though, garbage collection is the way to go. On today’s powerful computer systems, the overhead associated with garbage collection is practically meaningless, and on multi-core and multi-processor machines, garbage collection can actually result in applications that perform better than those requiring manual memory management, because the garbage collector will take advantage of unused cores or processors for recovering memory. Additionally, you’ll save time from not having to worry about or debug memory-related issues.

NOTE: Garbage collection in Objective-C is a topic not without some controversy. Some developers have been resistant to using garbage collection, preferring to do manual memory management. With the improvements to the multi-processing architecture and garbage collector that arrived with Snow Leopard, however, the case for garbage collection has become very compelling, and it should be used unless you have a very good reason not to.

Creating Our Controller Class


Now that we’ve got our project set up to use garbage collection, it’s time to create our controller class. Expand the Classes folder in the Groups & Files pane. If you’re running Snow Leopard, you’ll see a pair of files called ButtonsAppDelegate.h and ButtonsAppDelegate.m that define your application’s main controller class. They were created for you when you created the Xcode project, and you can skip on down to the “Declaring an Outlet and Action” section. However, if you’re still running Leopard, your version of Xcode doesn’t create that class for you, so that folder will be empty. The disclosure triangle will expand, but nothing else will change. That’s because there are no classes in your project yet. Our application is going to need a single controller class, however, so we’re going to create that class now. Single-click the Classes folder to select it and select New File… from the File menu, or press ⌘N. This will bring up the new file assistant (Figure 3-3).

Figure 3.3. The New File assistant allows you to choose from a large number of different file templates. This assistant looks somewhat different in Snow Leopard, where selecting the Objective-C class icon reveals a popup, letting you choose one of several classes to subclass. But for the most part they are pretty similar.

In the list on the left-hand side of the new file assistant, under the Mac OS X heading, select Cocoa. In the upper-right pane, you’ll now see a bunch of icons representing different file templates commonly used in Cocoa applications. Look for one called Objective-C class, and select it. When prompted for a name, call it ButtonsAppDelegate.m, and make sure the checkbox called Also creates “ButtonsAppDelegate.h” is checked. Click the Finish button, and your Classes folder in the Groups & File pane should now have two items in it. These files define the class that we will use as the controller for our application’s main window.

NOTE: If you’ve programmed in Cocoa Touch, you may be surprised that we’re subclassing NSObject rather than a provided view controller class. Cocoa, like Cocoa Touch, does have generic controller classes, and you’ll see them and learn when to use them in later chapters. Cocoa Touch was built from the ground up around the concept of generic view controllers, but Cocoa had been around for many years when they were first implemented, so Cocoa doesn’t have an equivalent class to Cocoa Touch’s UIViewController. As a result, the main controller classes in Cocoa are typically subclasses of NSObject.

Declaring an Action and Outlet


Now we need to edit one of those new files to declare our outlets and actions. Because you just created the two files, they are probably both selected. Single-click the Classes folder to deselect them, and then click on ButtonsAppDelegate.h. This should bring up that file in the editing pane. We need to add declarations for an outlet and an action method. Interface Builder only looks at the header (.h) file when it looks for the IBOutlet and IBAction keywords, so once we’ve declared the action

Return Main Page Previous Page Next Page

®Online Book Reader