Objective C Tutorial - Objective-C Program Structure






A Objective-C program basically consists of the following parts:

  • Preprocessor Commands

  • Interface

  • Implementation

  • Method

  • Variables

  • Statements

  • Comments





Example

Let us look at a simple code that would print the words "Hello World":


#import <Foundation/Foundation.h>

@interface SampleClass:NSObject
- (void)sampleMethod;
@end

@implementation SampleClass

- (void)sampleMethod{
   NSLog(@"Hello, World! \n");
}

@end

int main()
{
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass sampleMethod];
   return 0;
}




Preprocessor

The first line of the program #import <Foundation/Foundation.h> is a preprocessor command.

It tells a Objective-C compiler to include Foundation.h file.

This is a system file-not a file that we created.

#import says to include the information from that file into the program, exactly as if the contents of the file were typed into the program at that point.

You imported the file Foundation.h because it has information about other classes and functions that are used later in the program.

@interface

@interface SampleClass:NSObject shows how to create an interface.

:NSObject means to inherit from NSObject, which is the base class of all objects.

- (void)sampleMethod; shows how to declare a method.

@end marks the end of an interface.

@implementation

@implementation SampleClass shows how to implement the interface SampleClass.

- (void)sampleMethod{} shows the implementation of the sampleMethod.

@end marks the end of an implementation.

main method

int main() is the main function where program execution begins.

/*...*/ is ignored by the compiler and acts as additional comments in the program.

NSLog(...) is a function from in Objective-C which outputs "Hello, World!" to the screen.

return 0; terminates main()function and returns the value 0.

Comments

The lines that start with two slash characters // are called comments.

A comment statement is used in a program to document a program and enhance its readability.

We can insert comments into an Objective-C program in two ways.

One is by using two consecutive slash characters //.

The compiler ignores any characters that follow these slashes, up to the end of the line.

We can also initiate a comment with the /*. This marks the beginning of the comment. To end the comment, you use the characters */, without any embedded spaces.

All characters included between the opening /* and the closing */ are treated as part of the comment statement and are ignored by the Objective-C compiler.

This form of comment is often used when comments span many lines of code, as in the following:

/*
This file implements a class called Fraction, which
represents fractional numbers. 
*/

Just note that you cannot nest the /* style comments.

Using variables

#import <Foundation/Foundation.h>
 
int main ()
{

   int myAge = 49;
    float myPaycheck = 5120.75;
    NSLog (@"This is my age: %i", myAge);
    NSLog (@"This is my paycheck amount: %f", myPaycheck);  
   return 0;
}

The code above generates the following result.