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

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

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.

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​ ​{​

 ​ ​ ​ ​/​/​ ​i​n​s​e​r​t​ ​c​o​d​e​ ​h​e​r​e​.​.​.​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​H​e​l​l​o​,​ ​W​o​r​l​d​!​​n​"​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​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 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.

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​t​h​e​ ​b​e​g​i​n​n​i​n​g​ ​o​f​ ​t​h​e​ ​n​o​v​e​l​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​t​ ​w​a​s​ ​t​h​e​ ​b​e​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​t​ ​w​a​s​ ​t​h​e​ ​w​o​r​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​)​;​
 ​ ​ ​ ​/​*​ ​I​s​ ​t​h​a​t​ ​a​c​t​u​a​l​l​y​ ​a​n​y​ ​g​o​o​d​?​
 ​ ​ ​ ​ ​ ​ ​M​a​y​b​e​ ​i​t​ ​n​e​e​d​s​ ​a​ ​r​e​w​r​i​t​e​.​ ​*​/​

 ​ ​ ​ ​r​e​t​u​r​n​ ​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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset