Cocoa Programming for Mac OS X - Aaron Hillegass [8]
Aaron used to have a boss named Rock. Rock had earned a degree in astrophysics from Cal Tech and had never had a job that used his knowledge of the heavens. He was once asked if he regretted getting the degree. “Actually, my degree in astrophysics has proved to be very valuable,” he said. “Some things in this world are just hard. When I am struggling with something, I sometimes think ‘Damn, this is hard for me. I wonder if I am stupid,’ and then I remember that I have a degree in astrophysics from Cal Tech; I must not be stupid.”
Before going any further, assure yourself that you are not stupid and that some things are just hard. Armed with this silly affirmation and a well-rested mind, you are ready to conquer Cocoa.
Chapter 2. Let’s Get Started
Many books would start off by giving you a lot of philosophy. This would be a waste of precious paper at this point. Instead, we are 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: Seed random number generator using time and Generate random number. A text field will display the generated number. This simple example involves taking user input and generating output. At times, the description of what you are doing and why will seem, well, terse. Don’t worry—we will explore all 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 Application
In Xcode
Assuming that you have installed the Developer Tools, you will find Xcode in /Developer/Applications/. You will be using the application a lot, so drag it to the dock at the bottom of your screen. Launch Xcode. (If you have never run Xcode before, you may get a Welcome page. Just take all the defaults and click through.)
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, then New Project.... When the panel appears (see Figure 2.2), choose the type of project you would like to create: Cocoa Application. Note that many other types of projects are 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 (also called a plug-in) is dynamically loaded at runtime. An application typically links against a framework at compile time.
For the project name, type in Random, as in Figure 2.3. Application names are typically capitalized. Set the Class Prefix to Random and uncheck Create Document-Based Application, Use Core Data, and Include Unit Tests. Make sure that Use Automatic Reference Counting is checked. We will use this setting in every new project in this book.
Figure 2.3. Name Project
Next, you will pick the directory in which your project directory will be created. By default, your project directory will be created inside your home directory. Uncheck Create local git repository for this project. Click the Create button.
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 a file in your project.