Objective-C: Structure of program
You can use X-Code to write your first program ‘Hello World!’:
// First program example
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Hello, World!");
[pool drain];
return 0;
}
The first line of the program is a comment:
// 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:
/*
A multi-line comment can be inserted
by using a slash
and then an asterisk.
The comment can be as long as you want,
but make sure you close it
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:
#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:
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:
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.
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.
return 0;
66,142 total views, 1 views today