Online Book Reader

Home Category

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

By Root 477 0
be written in a given programming language. These rules are extremely specific, and if you fail to follow them, your program won’t work.

While the syntax regarding comments is fairly simple, the syntax of code can vary widely depending on what the code does and how it does it. But there’s one feature that remains consistent: every statement ends in a semicolon. (We’ll see examples of code statements in just a moment.) If you forget a semicolon, you will have made a syntax error, and your program won’t work.

Fortunately, Xcode has ways to warn you of these kinds of errors. In fact, one of the first challenges you will face as a programmer is interpreting what Xcode tells you when something goes wrong and then fixing your errors. You’ll get to see some of Xcode’s responses to common syntax errors as we go through the book.

Let’s make some changes to main.c. First, we need to make some space. Find the curly braces ({ and }) that mark the beginning and the end of the main function. Then delete everything in between them.

Now update main.c to look like the code below. You’ll add a comment, two lines of code, and another comment to the main function. For now, don’t worry if you don’t understand what you are typing. The idea is to get started. You have an entire book ahead to learn what it all means.

#include

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

{

// Print the beginning of the novel

printf("It was the best of times.\n");

printf("It was the worst of times.\n");

/* Is that actually any good?

Maybe it needs a rewrite. */

return 0;

}

(Notice that the new code you need to type in is shown in a bold font. The code that isn’t bold is code that is already in place. That’s a convention we’ll use for the rest of the book.)

As you type, you may notice that Xcode tries to make helpful suggestions. This feature is called code completion, and it is very handy. You may want to ignore it right now and focus on typing things in all yourself. But as you continue through the book, start playing with code completion and how it can help you write code more conveniently and more accurately. You can see and set the different options for code completion in Xcode’s preferences, which are accessible from the Xcode menu.

In addition, keep an eye on the font color. Xcode uses different font colors to make it easy to identify comments and different parts of your code. (For example, comments are green.) This comes in handy, too: after a while of working with Xcode, you begin to instinctively notice when the colors don’t look right. Often, this is a clue that there is a syntax error in what you’ve written (like a forgotten semi-colon). And the sooner you know that you’ve made a syntax error, the easier it is to find and fix it. These color differences are just one way in which Xcode lets you know when you (may) have done something wrong.

How do I run my program?


When the contents of your main.c file match what you see above, it’s time to run your program and see what it does. This is a two-step process. Xcode builds your program and then runs it. When building your program, Xcode prepares your code to run. This includes checking for syntax and other kinds of errors.

Look again at the lefthand area of the Xcode window. This area is called the navigator area. At the top of the navigator area is a series of buttons. You are currently viewing the project navigator, which shows you the files in your project. The project navigator’s icon is .

Now find and click the button to reveal the log navigator. The log is Xcode’s way of communicating with you when it is building and running your program.

You can also use the log for your own purposes. For instance, the line in your code that reads

printf("It was the best of times.\n");

is an instruction to display the words “It was the best of times.” in the log.

Since you haven’t built and run your program yet, there isn’t anything in the log navigator. Let’s fix that. In the upper lefthand corner of the project window, find the button that looks suspiciously

Return Main Page Previous Page Next Page

®Online Book Reader