Objective-C Tutorial 1 – Set Up Xcode, Output Hello World!, And Make Comments

4 minute read

Objective-C is an object-oriented programming language that builds upon the C programming language. The language is most commonly used on Apple’s Macintosh operating system and Apple’s mobile operating system, iOS.

Before we start programming in Objective-C, we will want to set up our IDE. Xcode is the best IDE for Objective-C out there, so we’re going to go with that. In general, it’s best to always use the latest version of a piece of software. The latest version of Xcode (Xcode 4 as of the writing of this post) can be found in the Mac App Store ( link), and Apple charges USD $4.99 for it. If you aren’t able to shell out the cash, or if you are running a version of Mac OS younger than 10.6, you can sign up for a developer account at http://developer.apple.com/programs/register/ and then find an earlier version of Xcode at http://connect.apple.com (in the “Developer Tools” section).

Once you have downloaded Xcode, open it (if you downloaded it to the default location, it can be found in the root folder -> Developer -> Applications -> Xcode). Hit the “Create a new Xcode project” button. In the Mac OS X section of the sidebar, select “Application”. After that, choose “Command Line Tool” and, in the “Type” drop-down menu, choose “Foundation” (yes, Foundation as in the Foundation Series by Isaac Asimov =P ). Next, click the “Choose…” button and give your project a name. Save it, and your project window will open.

The Hello World program is the default code of a new project. It can be found in the main file (projectname.m). The code is as follows:

 #import 
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello, World!");
[pool drain];
return 0;
}

It would seem obvious that to run the program, you should click the “Build and Run” button. While this WILL run the program, you won’t actually be able to see any output! What you are going to want to do is go to (in the menu bar) Run -> Console. This will bring up the console window that will show you program output and will allow for input as well. Hit “Build and Run” and watch “Hello World!” be outputted to the console!

Now, let’s go through the program line by line and I will explain what you should know about this program.

#import

This imports a header file that allows the computer to access the functionality of the Foundation framework. To simplify what I just said…. er… typed, there is a file with a bunch of code in it that you don’t want to have to type and this imports all of that code into the program. However, this functionality isn’t used in this program (and will run fine without it).

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

This declares the main function, and is known as a “function header”. The main function is the starting point for all programs in Objective-C. It is what is run by the computer when the entire program itself is run. Functions in general are chunks of code that contain instructions for the computer. They can be called upon at any time from any other function. For now, don’t worry about the code in the parentheses. Lastly, the “{” (left curly brace) begins the body of the main function.

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

This line borrows some memory from your computer so that your program can run.

NSLog(@”Hello, World!”);

NSLog is used to display/output text on the screen. Anything in the quotation marks is the text that gets outputted.

[pool drain];

This line releases, or “drains”, the memory that we borrowed earlier in the NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; line.

return 0;

This line returns the value “0” to the computer. Every function, except for the void function, returns a value. This value is described by the data type declared in the function header. In this case, the data type is an int, or integer. Returning the value of “0” indicates that the program ran successfully.

}

The right curly brace is simply the end of the body function.

Comments

A comment is a section of text that isn’t run in a program. A single line comment is indicated by two forward slashes (//). All text after that on the same line won’t be run by the computer when it comes to it. Comments can be used to describe parts of your program, or to turn parts of your program off.

In addition to a single line comment, there are multi-line comments. Multi-line comments are comments that cover more than when line. They are started with a “/” and ended with a “/”.

Here’s an example of comments in use:

 #import 
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// this is a single line commment!
/*
this is a
multi-line
comment!
*/
[pool drain];
return 0;
}

That’s all for now folks, I hope that you found this tutorial helpful! I encourage you to play around with the code by changing the text outputted with NSLog and by testing out comments.

EDIT: Objective-C tutorial 2 is now up at: /2011/07/10/objective-c-tutorial-2-variables-and-data-types/ .

Leave a Comment