Getting Started with Cocoa Programming
Many books would start off by giving you a lot of philosophy. This would be a waste of precious paper at this point. Instead, I am going to guide you through writing your first Cocoa application. Upon finishing, you will be excited and confused. . .and ready for the philosophy.
Our first project will be a random number generator application. It will have two buttons labeled Seed random number generator with time and Generate random number. There will be a text field that will display the generated number. This is a simple example that involves taking user input and generating output. At times, the description of what you are doing and why will seem, well, terse. Don't worrywe will explore all of this in more detail throughout this book. For now, just play along.
Figure 2.1 shows what the completed application will look like.
Figure 2.1 Completed
In Xcode
Assuming you have installed the developer tools, you will find Xcode in /Developer/Applications/. Drag the application to the dock at the bottom of your screen; you will be using it a lot. Launch Xcode.
As mentioned earlier, Xcode will keep track of all the resources that go into your application. All these resources will be kept in a directory called the project directory. The first step in developing a new application is to create a new project directory with the default skeleton of an application.
Create a New Project
Under the File menu, choose New Project. . .. When the panel appears (see Figure 2.2), choose the type of project you would like to create: Cocoa Application. Notice that there are many other types of projects available as well.
Figure 2.2 Choose Project Type
In this book, we will discuss the following major types of projects:
-
Application: A program that creates windows.
-
Tool: A program that does not have a graphical user interface. Typically, a tool is a command-line utility or a daemon that runs in the background.
-
Bundle or Framework: A directory of resources that can be used in an application or tool. A bundle is dynamically loaded at runtime. An application typically links against a framework at compile time.
For the project name, type in RandomApp, as in Figure 2.3. Application names are typically capitalized. You can also pick the directory into which your project directory will be created. By default, your project directory will be created inside your home directory. Click the Finish button.
Figure 2.3 Name
A project directory will be created for you, with the skeleton of an application inside it. You will extend this skeleton into the source for a complete application and then compile the source into a working application.
Looking at the new project in Xcode, you will see an outline view on the left side of the window. Each item in the outline view represents one type of information that might be useful to a programmer. Some items are files, others are messages like compiler errors or find results. For now, you will be dealing with editing files, so open the item that says RandomApp to see folders that contain the files that will be compiled into an application.
The skeleton of a project that was created for you will actually compile and run. It has a menu and a window. Click on the toolbar item with the hammer and green circle to build and run the project as shown in Figure 2.4.
Figure 2.4 Skeleton of a Project
While the application is launching, you will see a bouncing icon in the dock. The name of your application will then appear in the menu. This means that your application is now active. The window for your application may be hidden by another window. If you do not see your window, choose Hide Others from the RandomApp menu. You should see an empty window as shown in Figure 2.5.
Figure 2.5 Running the Project
It doesn't do much, but notice that it is already a fully functional application. Printing even works. Quit RandomApp and return to Xcode.
The main Function
Select main.m by single-clicking on it. If you double-click on the filename, it will open in a new window. Because I deal with many files in a day, this tends to overwhelm me rather quickly, so I use the single-window style. Click on the Editor toolbar item to split the window and create an editor view. The code will appear in the editor view (Figure 2.6).
Figure 2.6 main()
You will almost never modify main.m in an application project. The default main() simply calls NSApplicationMain(), which in turn loads the user interface objects from a nib file. Nib files are created with Interface Builder. (Trivia: "NIB" stands for "NeXT Interface Builder"; "NS" stands for "NeXTSTEP.") Once your application has loaded the nib file, it simply waits for the user to do something. When the user clicks or types, your code will be called automatically. If you have never written an application with a graphical user interface before, this change will be startling to you: The user is in control, and your code simply reacts to what the user does.