Objective-C: Structure of program

Objective-C: Structure of program

You can use X-Code to write your first program ‘Hello World!’:

  1. // First program example
  2. #import <Foundation/Foundation.h>
  3. int main (int argc, const char * argv[])
  4. {
  5.         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  6.         NSLog (@"Hello, World!");
  7.         [pool drain];
  8.         return 0;
  9. }

The first line of the program is a comment:

  1. // First program example

Comments exist solely for the benefit of the programmer. Comments are important, because they allow the developer to explain a thought process. They help demystify the code, and make documenting the code sometime down the road easier. Comments help during the debugging process, or when you revisit your code some time later. It also helps other people who may use your code understand why things are the way the are. The compiler ignores comments, so you can type anything into a comment.

There are two types of comments in Objective-C. One is shown above, in which every line of the comment is preceded by two slashes. This type of comment ends at the end of the line.

A comment can also be written across multiple lines, with the following syntax:

  1. /*
  2. A multi-line comment can be inserted
  3. by using a slash
  4. and then an asterisk.
  5. The comment can be as long as you want,
  6. but make sure you close it
  7. with an asterisk, then a slash. */

Which style you choose is a matter of preference, or as the situation dictates.

The next line of the program imports, or brings into your project, a file called Foundation.h:

  1. #import <Foundation/Foundation.h>

The file in question is a system file, and that is why the name is enclosed in the brackets < and >. If you were importing a local file , you would enclose the file name in double quotes “ and ”. This file is imported because code later in the file requires information that is contained in this file; you are telling the compiler to look up the information in that file as necessary.

The following line declares a function called main:

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

main is a function that is where every C or C-based program begins. It is a reserved name, which means that you can’t have a function named main. The word int that precedes main is a declaration of the return type of the function. These topics will be discussed in further chapters.

After identifying main to the compiler, you tell the system what to do when it is called. These statements are enclosed in the braces { and } that surround the next few lines. Every opening brace has to be matched with a closing brace.

The first statement in main is as follows:

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

This involves memory management, which is a topic thoroughly discussed in upcoming chapters.

The next line calls NSLog, which, along with the NSAutoreleasePool from above, is a function brought in by Foundation.h, which you imported.

  1. NSLog (@"Hello, World!");

This line tells the function NSLog, which is designed to output text, to print the characters Hello, World! on the screen.

The leading @ in front of the string signifies to the compiler that this is an NSString (another object brought in from the Foundation.h file), not a C-style string. For the most part, you will be using NSStrings, rather than C-style strings.

All statements, or lines that indicate some action, must be terminated with a semicolon ; just like regular sentences are terminated with a period.

The next line is part of the NSAutoreleasePool, and its purpose and meaning will be discussed in a later chapter. The final line tells the main method to return the value 0. Remember that the int the preceded main tells the system that this function will return a value. This value is 0. By convention, a return value of zero indicates that the function was successful.

  1. return 0;

62,028 total views, 3 views today