Home > Articles > Programming > Mac and iOS Programming

  • Print
  • + Share This
This chapter is from the book

Hello World

Just to get oriented, I’m going to start with the simplest imaginable example project—so simple, you won’t have to do much coding at all.

A New Project

Click the Create a new Xcode project link. Xcode opens an empty Workspace window, and drops the New Project assistant sheet in front of it (see Figure 2.2). Select OS X→Application from the list at left, and then the Command Line Tool template from the icons that appear at right. Click Next.

Figure 2.2

Figure 2.2 The New Project assistant leads you through the creation of an Xcode project. First, you specify the kind of product you want to produce.

The next panel (Figure 2.3) asks for the name of the project and the kind of command-line tool you want.

Figure 2.3

Figure 2.3 The Options panel of the New Project assistant lets you identify the product and what support it needs from system libraries.

  1. Type Hello World in the Product Name field. This will be used as the name of the project and its principal product.
  2. Xcode should have filled the Organization Name in for you, from your “me” card in the Address Book. If you listed a company for yourself, that’s what will be in the field; otherwise, it’s your personal name. Xcode will use this as the name of the copyright holder for all new files.
  3. Virtually all executable objects in the OS X and iOS world have unique reverse-DNS-style identifiers that are used to uniquely identify them. The Company Identifier is the leading portion of those reverse-DNS names, to be used by every product of this project. For this example, I used com.wt9t.
  4. By default, Xcode infers the unique Bundle Identifier from the company identifier and the name of the product. You’ll see later how to change this if you have to.
  5. The Type popup prompts Xcode on how to fill in the system libraries the tool will use. This is just a plain old C program, with no need for C++ or Apple-specific support, so choose C.

Finally, a put-file sheet appears, so you can select a directory to put the project into. For this example, select your Desktop. One more thing—uncheck the box labeled Create local git repository for this project. Source control (Chapter 7, “Version Control”) is a Good Thing, but let’s not deal with it in this trivial example. Click Create.

If you look on your Desktop, you’ll find that Xcode has created a folder named Hello World. The project name you specified is used in several places.

  • It’s the name of the project directory that contains your project files.
  • It’s the name of the project document (Hello World.xcodeproj) itself.
  • It’s the name of the product—in this case a command-line tool named Hello World.
  • It’s the name of the target that builds the product. I’ll get into the concept of a target later; for now, think of it as the set of files that go into a product, and a specification of how it is built.
  • It’s the name of the target’s directory inside the project’s directory.
  • Suitably modified, it’s the name of the man-file template for the tool (Hello World.1).

When you’ve made your choices, Xcode unmasks the workspace for the Hello World project (Figure 2.4). Don’t look at it too closely just yet. Xcode starts you off with a view of the settings that control how Hello World is to be built. This is useful information, but for now, it’s just details.

Figure 2.4

Figure 2.4 Once set up, the Hello World project window fills in with a list of source files and a display of the options that control how the application will be built.

More interesting is the program code itself. The left column of the window is called the Navigator area. Find main.c in the list, and click it (see Figure 2.5). The Editor area, which fills most of the window, now displays the contents of main.c. This is the code for the canonical simplest-possible program, known as “Hello, World.”

Figure 2.5

Figure 2.5 Clicking the name of an editable file in the Project navigator displays its contents in the Editor area.

The Navigator area displays many different things in the course of development—it’s not just a file listing. It can display analyses, searches, and build logs. Which list you see often depends on what Xcode wants to show you; you can make the choice yourself by clicking the tiny icons in the bar at the top of the Navigator area. Hovering the mouse pointer over them will show you the names of the various views.

As this book goes on, you’ll meet all of them. For now, you care only about the “Project” navigator, the file list Xcode starts you out with. Feel free to click the other icons, but to keep up with this example, be sure to return to the Project navigator, as shown in Figure 2.5.

Quieting Xcode Down

But first.

Xcode is a toolset that contains everything its creators could think of to provide a powerful, helpful environment for writing iOS and OS X applications. Often, you barely need to begin a task, and Xcode will offer to finish it for you. It will usually be right. I use these features all the time. I recommend them.

You’re going to turn them all off.

Automatic completions and indentations and code decorations and code fixes are great, once you know what’s going on, but an automaton that snatches your work out of your hands, however helpfully, is straight out of The Sorcerer’s Apprentice. Better to start with what you want to do; once you’re confident of what that is, then you have the discretion and control to direct Xcode as it helps you.

So you’re going to damp Xcode down a bit. You’ll do all of this in Xcode’s Preferences window, which you can summon with Xcode→Preferences. . . (Imagecomma). The Preferences window is divided into panels, which you select with the icons at the top of the window.

To start, make sure the General panel is visible. Under Issues, uncheck Show live issues.

Next, select the Text Editing panel, which has two tabs. Select the Editing tab, and uncheck Show: Code folding ribbon, and all the options under Code completion:.

In the Indentation tab, turn off Line wrapping: Wrap lines to editor width and the Syntax-aware indenting section.

Now Xcode will mostly stay out of your way as you explore.

Building and Running

The program in main.c would run as is, but we have to trick Xcode into keeping its output on the screen long enough to see it. Add a few lines after the printf call so it looks like this:

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

{



    // insert code here...

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



    /******************************************

     *  Pause, so the console doesn't disappear

     ******************************************/

    char     dummy[128];

    fgets(dummy,  sizeof(dummy), stdin);



    return 0;

}

Now we can run it. Select Product→Run (cmd.jpgR).

In the ordinary course, Xcode would then build and run Hello World. However, if this is the first time you’ve run any application, there is a security problem: Running an app from Xcode puts it under the observation of a debugger, which will have access to the internal data and running state of the app. Crossing process boundaries like that is technically a security breach, and you have to authorize it.

Xcode posts an alert, Enable Developer Mode on this Mac?. It explains that you could be asked for an administrator’s password every time you run the debugger (“Developer Tools Access needs to take control of another process. . . ”), or, with Developer Mode, you could do the authorization once and forget about it. Click Enable, enter an administrator’s credentials, and carry on.

With authorization taken care of, a heads-up window (“bezel”) appears almost instantly, to tell you “Build Succeeded.” (If Xcode is in the background, a notification banner will appear saying the build succeeded, and identifying the project and product involved.)

So. What happened?

Hello World is a console application; it just writes out some text without putting up any windows. Xcode captures the console of the apps it runs in the Debug area, which popped into view when you ran the program (Figure 2.6). The Debug area includes a console view on the right. It says Hello, World! (Figure 2.7).

Figure 2.6

Figure 2.6 The View selector in the toolbar shows and hides the Navigator, Debug, and Utility areas (left to right) of the project window. Clicking a button so it is highlighted exposes the corresponding area. Here, the Navigator and Debug areas are selected.

Figure 2.7

Figure 2.7 Opening the Debug area after running Hello World shows the eponymous output.

Click in the console to make it ready for text input, and press the Return key. Hello World exits, and the Debug area closes.

The Real Thing

What Xcode just produced for you is a real, executable application, not a simulation. To prove it, open the Terminal application (you’ll find it at /Applications/Utilities/Terminal, and you’d be well advised to add Terminal to your Dock). In Xcode, find the Hello World product in the Project navigator by clicking the disclosure triangle next to the Products folder icon. Drag the Hello World icon into the Terminal window, switch to Terminal, and press the Return key. (The path to a file deep in a directory for build products is remarkably long, but Terminal takes care of the necessary escaping.) “Hello, World!” appears.

If you want access to the executable file itself, select it in the Project navigator, then File→Show in Finder—also available in the contextual menu you get by right-clicking the Hello World icon. A window will open in the Finder showing you the file.

You’re done! You can close the Workspace window (File→Close Project, cmd-2.jpgW) or quit Xcode entirely (Xcode→Quit Xcode, cmd.jpgQ).

  • + Share This
  • 🔖 Save To Your Account