Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [4]

By Root 468 0
be writing mostly command-line tools to focus on programming essentials without the distraction of creating and managing a user interface.

Now we’re going to create a simple command-line tool using Xcode so you can see how it all works.

When you write a program, you create and edit a set of files. Xcode keeps track of those files in a project. Launch Xcode. From the File menu, choose New and then New Project….

To help you get started, Xcode suggests a number of possible project templates. You choose a template depending on what sort of program you want to write. In the lefthand column, select Application from the Mac OS X section. Then choose Command Line Tool from the choices that appear to the right.

Figure 2.1 Choosing a template

Press the Next button.

Name your new project AGoodStart. The company identifier won’t matter for our exercises in this book, but you have to enter one here to continue. You can use BigNerdRanch or another name. From the Type pop-up menu, choose C because you will write this program in C.

Figure 2.2 Choose options

Press the Next button.

Now choose the folder in which your project directory will be created. You won’t need a repository for version control, so you can uncheck that box. Finally, click the Create button.

You’ll be creating this same type of project for the next several chapters. In the future, I’ll just say, “Create a new C Command Line Tool named program-name-here” to get you to follow this same sequence.

(Why C? Remember, Objective-C is built on top of the C programming language. You’ll need to have an understanding of parts of C before we can get to the particulars of Objective-C.)

Where do I start writing code?


After creating your project, you’ll be greeted by a window that shows how AGoodStart will be produced.

Figure 2.3 First view of the AGoodStart project

This window includes details like which versions of Mac OS X can run your application, the configurations to use when compiling the code that you write, and any localizations that have been applied to your project. But let’s ignore those details for now and find a simple starting point to get to work.

Near the top of the lefthand panel, find a file called main.c and click on it. (If you don’t see main.c, click the triangle next to the folder labeled AGoodStart to reveal its contents.)

Figure 2.4 Finding main.c in the AGoodStart group

Notice that our original view with the production details changes to show the contents of main.c. The main.c file contains a function called main.

A function is a list of instructions for the computer to execute, and every function has a name. In a C or Objective-C program, main is the function that is called when a program first starts.

#include

int main (int argc, const char * argv[]) {

// insert code here...

printf("Hello, World!\n");

return 0;

}

In this function, you’ll find the two kinds of information you write in a program: code and comments.

Code is the set of instructions that tell the computer to do something.

Comments are ignored by the computer, but we programmers use them to document code we’ve written. The more difficult the programming problem you are trying to solve, the more comments will help document how you solved the problem. That becomes especially important when you return to your work months later, look at code you forgot to comment, and think, “I’m sure this solution is brilliant, but I have absolutely no memory of how it works.”

In C and Objective-C, there are two ways to distinguish comments from code:

If you put // at the beginning of a line of code, everything from those forward slashes to the end of that line is considered a comment. You can see this used in Apple’s “insert code here...” comment.

If you have more extensive remarks in mind, you can use /* and */ to mark the beginning and end of comments that span more than one line.

These rules for marking comments are part of the syntax of C. Syntax is the set of rules that governs how code must

Return Main Page Previous Page Next Page

®Online Book Reader