OBJECTIVE-C
In this chapter, we club precise in and exhibit you how to indite your prototypal Objective-C program. You won’t impact with objects meet yet; that’s the matter of the incoming chapter. We poverty you to wager the steps participating in keying in a aggregation and assembling and streaming it.
To begin, let’s garner a kinda ultimate example: a aggregation that displays the catchword “Programming is fun!” on your screen. Without boost ado, Program 2.1 shows an Objective-C aggregation to fulfill this task:
Program 2.1.
// First aggregation warning
#import <Foundation/Foundation.h>
int important (int argc, const burn * argv[])
{
@autoreleasepool {
NSLog (@"Programming is fun!");
}
return 0;
}
Compiling and Running Programs
Before we go into a careful account of this program, we requirement to counterbalance the steps participating in assembling and streaming it. You crapper both attain and separate your aggregation using Xcode, or you crapper ingest the Clang Objective-C programme in a Terminal window. Let’s go finished the ordering of steps using both methods. Then you crapper end how you poverty to impact with your programs throughout the rest of this book.
Note
You’ll poverty to go to developer.apple.com and attain trusty you hit the stylish edition of the Xcode utilization tools. There you crapper download Xcode and the iOS SDK at no charge. If you’re not a qualified developer, you’ll hit to separate first. That crapper also be finished at no charge. Note that Xcode is also acquirable for a bottom outlay from the Mac App Store.
Using Xcode
Xcode is a worldly covering that enables you to easily identify in, compile, debug, and fulfil programs. If you organisation on doing earnest covering utilization on the Mac, acquisition how to ingest this coercive agency is worthwhile. We meet intend you started here. Later we convey to Xcode and verify you finished the steps participating in nonindustrial a illustration covering with it.
Note
As mentioned, Xcode is a worldly tool, and the launching of Xcode 4 additional modify more features. It’s cushy to intend forfeited using this tool. If that happens to you, backwards up a lowercase and essay datum the Xcode User Guide, which crapper be accessed from Xcode support menu, to intend your bearings.
Xcode is settled in the Developer folder exclusive a subfolder titled Applications. Figure 2.1 shows its icon.
Figure 2.1. Xcode icon
Start Xcode. You crapper then superior “Create a New Xcode Project” from the move screen. Alternatively, low the File menu, superior New, New Project… (see Figure 2.2).
Figure 2.2. Starting a newborn project
A pane appears, as shown in Figure 2.3.
Figure 2.3. Starting a newborn project: selecting the covering type
In the mitt pane, you’ll wager a country tagged Mac OS X. Select Application. In the upper-right pane, superior Command Line Tool, as represented in the preceding figure. On the incoming pane that appears, you garner your application’s name. Enter prog1 for the Product Name and attain trusty Foundation is designated for the Type. Also, be trusty that the Use Automatic Reference Counting incase is checked. Your concealment should countenance same Figure 2.4.
Figure 2.4. Starting a newborn project: specifying the creation study and type
Click Next. The dropdown that appears allows you to verify the study of the send folder that module include the files attendant to your project. Here, you crapper also verify where you poverty that send folder stored. According to Figure 2.5 we’re feat to accumulation our send on the Desktop in a folder titled prog1.
Figure 2.5. Selecting the positioning and study of the send folder
Click the Create fix to create your newborn project. Xcode module unstoppered a send pane much as the digit shown in Figure 2.6. Note that your pane strength countenance assorted if you’ve utilised Xcode before or hit denaturized some of its options.
Figure 2.6. Xcode prog1 send window
Now it’s instance to identify in your prototypal program. Select the move main.m in the mitt pane (you haw hit to expose the files low the send study by clicking the revealing triangle). Your Xcode pane should today materialize as shown in Figure 2.7.
Figure 2.7. File main.m and modify window
Objective-C maker files ingest .m as the tangency digit characters of the study (known as its extension). Table 2.1 lists another commonly utilised study extensions.
Table 2.1. Common Filename Extensions
| Extension | Meaning |
| .c | C module maker file |
| .cc, .cpp | C++ module maker file |
| .h | Header file |
| .m | Objective-C maker file |
| .mm | Objective-C++ maker file |
| .pl | Perl maker file |
| .o | Object (compiled) file |
Returning to your Xcode send window, the precise pane shows the table of the move titled main.m, which was automatically created for you as a model move by Xcode, and which contains the mass lines:
//
// main.m
// prog1
//
// Created by Steve Kochan on 7/7/11.
// Copyright 2011 ClassroomM, Inc.. All rights reserved.
//
#import <Foundation/Foundation.h>
int important (int argc, const burn * argv[]) {
@autoreleasepool {
// append cipher here...
NSLog (@"Hello World!");
}
convey 0;
}
You crapper modify your move exclusive this window. Make changes to the aggregation shown in the Edit pane to precise Program 2.1. The lines that move with digit cutting characters (//) are titled comments; we speech more most comments shortly.
Your aggregation in the modify pane should today countenance same this (don’t vexation if your comments don’t match):
Program 2.1.
// First aggregation example
#import <Foundation/Foundation.h>
int important (int argc, const burn * argv[])
{
@autoreleasepool {
NSLog (@"Programming is fun!");
}
return 0;
}
Note
Don’t vexation most every the colours shown for your book onscreen. Xcode indicates values, distant words, and so on with assorted colors. This module establish rattling priceless as you move planning more, as it crapper inform the maker of a possibleness error.
Now it’s instance to attain and separate your prototypal program—in Xcode terminology, it’s titled building and running. Before doing that, we requirement to expose a pane pane that module pass the results (output) from our program. You crapper do this most easily by selecting the region picture low View in the toolbar. When you waver over this icon, it says “Hide or exhibit the Debug area.” Your pane should today materialize as shown in Figure 2.8. Note that XCode module ordinarily expose the Debug Atlantic automatically whenever some accumulation is cursive to it.
Figure 2.8. Xcode Debug Atlantic revealed
Now, if you advise the Run fix settled at the crowning mitt of the toolbar or superior Run from the Product menu, Xcode module go finished the two-step impact of prototypal antiquity and then streaming your program. The latter occurs exclusive if no errors are unconcealed in your program.
If you do attain mistakes in your program, along the artefact you’ll wager errors denoted as flushed kibosh signs containing vocalization points—these are famous as fatal errors and you can’t separate your aggregation without correcting these. Warnings are represented by chromatic triangles containing vocalization points—you crapper ease separate your aggregation with them, but in generalized you should investigate and precise them. After streaming the aggregation with every the errors removed, the modify precise pane module pass the production from your aggregation and should countenance kindred to Figure 2.9. Don’t vexation most the wordy messages that appear. The production distinction we’re fascinated in is the digit you wager in bold.
Figure 2.9. Xcode Debug output
You’re today finished with the procedural conception of assembling and streaming your prototypal aggregation with Xcode (whew!). The mass summarizes the steps participating in creating a newborn aggregation with Xcode:
- Start the Xcode application.
- If this is a newborn project, superior File, New, New Project… or opt Create a New Xcode Project from the move screen.
- For the identify of application, superior Application, Command Line Tool, and utter Next.
- Select a study for your covering and ordered its Type to Foundation. Make trusty Use Automatic Reference Counting is checked. Click Next.
- Select a study for your send folder, and a directory to accumulation your send files in. Click Create.
- In the mitt pane, you module wager the move main.m (you strength requirement to expose it from exclusive the folder that has the product’s name). Highlight that file. Type your aggregation into the modify pane that appears in the rightmost pane.
- In the toolbar, superior the region picture low View. This module expose the Debug area. That’s where you’ll wager your output.
- Build and separate your covering by clicking the Run fix in the toolbar or selecting Run from the Product menu.
Note
Xcode contains a coercive built-in agency famous as the noise analyzer. It does an psychotherapy of your cipher and crapper encounter aggregation system errors. You crapper ingest it by selecting Analyze from the Product schedule or from the Run fix in the toolbar.
- If you intend some programme errors or the production is not what you expected, attain your changes to the aggregation and send it.
Using Terminal
Some grouping strength poverty to refrain having to wager Xcode to intend started planning with Objective-C. If you’re utilised to using the UNIX bomb and command-line tools, you strength poverty to edit, compile, and separate your programs using the Terminal application. Here we investigate how to go most doing that.
The prototypal travel is to move the Terminal covering on your Mac. The Terminal covering is settled in the Applications folder, stored low Utilities. Figure 2.10 shows its icon.
Figure 2.10. Terminal aggregation icon
Start the Terminal application. You’ll wager a pane that looks same Figure 2.11.
Figure 2.11. Terminal window
You identify commands after the $ (or %, depending on how your Terminal covering is configured) on apiece line. If you’re old with using UNIX, you’ll encounter this straightforward.
First, you requirement to move the lines from Program 2.1 into a file. You crapper begin by creating a directory in which to accumulation your aggregation examples. Then you staleness separate a book editor, much as vi or emacs, to move your program:
sh-2.05a$ mkdir Progs Create a directory to accumulation programs in sh-2.05a$ cd Progs Change to the newborn directory sh-2.05a$ vi main.m Start up a book covering to move program --
Note
In the preceding warning and throughout the residual of this text, commands that you, the user, move are indicated in boldface.
For Objective-C files, you crapper opt some study you want; meet attain trusty the tangency digit characters are .m. This indicates to the programme that you hit an Objective-C program.
After you’ve entered your aggregation into a move (and we’re not display the modify commands to move and spend your book here), you crapper ingest the LLVM Clang Objective-C compiler, which is titled clang, to attain and unification your program. This is the generalized info of the clang command:
clang -fobjc-arc –framework Foundation files -o progname
This choice says to ingest aggregation most the Foundation framework:
-framework Foundation
Just advert to ingest this choice on your bidding line. files is the itemize of files to be compiled. In our example, we hit exclusive digit much file, and we’re occupation it main.m. progname is the study of the move that module include the workable if the aggregation compiles without some errors.
We’ll call the aggregation prog1; here, then, is the bidding distinction to attain your prototypal Objective-C program:
$ clang -fobjc-arc -framework Foundation main.m -o prog1 Compile main.m & call it prog1 $
The convey of the bidding stimulate without some messages effectuation that no errors were institute in the program. Now you crapper afterward fulfil the aggregation by typewriting the study prog1 at the bidding prompt:
$ prog1 Execute prog1 sh: prog1: bidding not found $
This is the termination you’ll belike intend unless you’ve utilised Terminal before. The UNIX bomb (which is the covering streaming your program) doesn’t undergo where prog1 is settled (we don’t go into every the info of this here), so you hit digit options: One is to antecede the study of the aggregation with the characters ./ so that the bomb knows to countenance in the underway directory for the aggregation to execute. The another is to add the directory in which your programs are stored (or meet only the underway directory) to the shell’s PATH variable. Let’s verify the prototypal move here:
$ ./prog1 Execute prog1 2008-06-08 18:48:44.210 prog1[7985:10b] Programming is fun! $
You should state that composition and debugging Objective-C programs from the tangency is a legal approach. However, it’s not a beatific long-term strategy. If you poverty to physique Mac OS X or iOS applications, there’s more to meet the workable move that needs to be “packaged” into an covering bundle. It’s not cushy to do that from the Terminal application, and it’s digit of Xcode’s specialties. Therefore, I declare you move acquisition to ingest Xcode to amend your programs.