In later chapters we will look at how to install and use objective-C on Windows and Linux systems for those that do not have access to Mac OS X. if you are planning to develop iPhone applications (or Mac OS X applications for that matter), however, you are
Going to need to use an Intel based Mac OS X system at some point in the future.
Perhaps the biggest advantage of using Mac OS x as your objective-C learning platform (aside from the ability to develop iPhone and Mac OS X applications) is the fact that you get to use Apple's xcode development tool. xcode is a powerful and easy to use
Development environment that is available free of charge to anyone fortunate enough to own an Apple computer running Mac OS X.
In this chapter we will cover the steps involved in installing xcode and writing and compiling a simple objective-C program in this environment. for those readers that prefer to do their coding and compiling at the command prompt we will then cover use
Objective-C in a terminal window.
Contents [Hide]
- 1 installing xcode on Mac OS X
- 2 starting xcode
- 3 starting a new project
- 4 writing an objective-C Application
With xcode
- 5 compiling objective-C from the command
Line
|
|
Installing xcode on Mac OS X
Xcode may or may not be pre-installed on your Mac OS X system. To find out if you already have it, open the finder and look for it inDeveloperSubfolder of
ApplicationsFolder. IfDeveloperFolder does not exist, or does not contain xcode then you will need to install it.
The best way to obtain xcode is to download it from the apple web site. it can also be installed from the developer tools installation disk if you happen to have one, but most MAC systems do not ship with this. the URL to download xcode ishttp: // developer.apple.com/groovy/xcode.html.
There are two forms of xcode, one for developing both Mac OS x and iPhone applications and the other solely for developing Mac OS X applications. in order to download xcode, you will need either a registered iPhone developer account (in the case of the iPhone
Xcode) or an ADC membership (in the case of the Mac OS X xcode ). fortunately, both of these memberships are free and can be activated using your existing Apple account (for example the one you use to buy music on iTunes ).
Once you have registered, you will gain access to the Apple Developer Connection web site where download links are provided for xcode on a variety of Mac OS X versions.
The download is over 2 GB in size and will take a number of hours to complete depending on the speed of your Internet connection. the package takes the form of a disk image (. DMG) file. once the download has completed, a new window will open as follows displaying
The contents of the. DMG file:
If this window does not open by default, it can be opened by clicking on the SDK disk drive icon on the desktop.
Initiate the installation by double clicking on the package icon (the one that looks like an opening box) and follow the instructions until you reachCustom installScreen:
The default selections on this screen are adequate for most requirements so unless you have specific needs there is no need to alter these selections. continue to the next screen, review the information and clickInstallTo begin the installation. Note
That you may first be prompted to enter your password as a security precaution. the duration of the installation process will vary depending on the speed and current load on the computer, but typically completes in 15-30 minutes.
Starting xcode
Having successfully installed the SDK and xcode, the next step is to launch it so that we can write and then compile a sample objective-C application. to start up xcode, open the finder, clickMacintosh HDDevice in the left had panel then double
Click onDeveloperFolder, followed byApplicationsFolder. Within this folder you shoshould see an icon for xcode. Double click on this icon to launch the tool. Once xcode has loaded, and assuming this is the first time you have used
Xcode on this system, you will be presented withWelcomeScreen. On this screen, click on the option
Create your first cocoa ApplicationTo proceed to the developer documentation screen.
Starting a new project
Each application created in xcode is contained withinProject. The first step in developing an application, therefore, is to create a new project. This is achieved by selectingFile-> new project...Menu option.
New projectWindow provides a range of different types of Mac OS applications that can be created. If you downloaded and installed the iPhone version of the SDK, options are also provided to create a new iPhone application:
To configure the project type, perform the following steps:
- Under the Mac OS X section, scroll down the list and selectCommand line utility.
- In the main panel, chooseFoundation ToolThen click onChoose ..Button.
- In the resulting panel, enterSampleappInSave:Field.
By default, xcode will put the project files in yourDocumentsFolder so be sure to create or specify a specific folder for your objective-C development work before proceeding. Once you have defined a name and location for your project clickSave
Button to continue. xcode will subsequently create the new project and open the main xcode window:
Writing an objective-C application with xcode
Xcode will create skeleton versions of the files needed to build a command-line based objective-C application. Objective-C source files are identified by. MFilename extension. In the case of our example, xcode has pre-created a main source file
NamedSampleapp. mAnd populated it with some basic code ready for us to edit. To view the code, selectSampleapp. mFrom the list of files so that the code appears in the editor area beneath the list. The skeleton Code reads as follows:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
ModifyNslogLine so that it reads:
NSLog(@"This is my first Objective-C App!");
With the change made, the next step is to compile and run the application by selectingBuild and runOption located in the xcode
BuildMenu. Once this option has been selected, xcode will compile the sources and run the application within a terminal window:
Compiling objective-C from the command line
While xcode provides a powerful environment that will prove invaluable for larger scale projects, for compiling a running such a simple application as the one we have been working with in this chapter it is a little bit of overkill. it is also a fact that
Some developers feel that development environments like xcode just get in the way and prefer to use a Basic Editor and command line tools to develop applications. after all, in the days before integrated development environments came into favor, this was how
All applications were developed. Whilst I'm not suggesting that everyone abandon xcode in favor ofVIEditor and GNU Compiler, it is useful to know that the option to work from the command line is available.
Using your favorite text or programming Editor, create a file namedHello. mContaining the following objective-C code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
[pool drain];
return 0;
}
Save the file and then open a terminal window (if you aren't already working in one), change directory to the folder containing the source file and compile the application with the following command:
gcc -framework Foundation hello.m -o hello
Assuming the Code compiles without error it can be run as follows:
./hello2009-09-25 15:51:11.528 hello[3371:10b] hello world
Compared to using xcode that seems much simpler, but keep in mind that the power of xcode really becomes evident when you start developing larger scale projects.
Before moving on to learn the basics of the objective-C programming language, we will cover the installation and use of objective-C on non-Mac OS X systems.