Header Ads

OBJECTIVE C -PROGRAM STRUCTURE

Before we study basic building block of the Objective-c programming language ,let us look a bare minimum Objective-c program structure so that we can take it as a reference in upcoming topics.

Objective -C Hello World Example
A Objective-c program basically consist of the following parts:
  • Preprocessor Commands
  • Interface
  • Implementation
  • Method
  • Variables
  • Statements & Expressions
  • Comments
Let us look at a simple code that would print the words "Hello Guys":

#import<Foundation/Foundation.h>                                                                                                         
@interface SampleClass:NSObject                                                                                                           
- (void)sampleMethod;                                                                                                                              
                                                                                                                                                                   
@implementation SampleClass                                                                                                                
- (void)sampleMethod{                                                                                                                             
           NSLog(@"Hello guys! \n");                                                                                                           
}                                                                                                                                                                     
@end                                                                                                                                                             
                                                                                                                                                                      
int main()                                                                                                                                                          
{                                                                                                                                                                    
     /*my first program in objective c */                                                                                                     
    SampleClass *sampleClass = [[SampleClass alloc] init];                                                                     
    [sampleClass sampleMethod];                                                                                                              
    return 0;                                                                                                                                                 
}                                                                                                                                                                     

Let us look various parts of the above program:
  1. The first line of the program
          #import <foundation/Foundation.h>
          is a preprocessor command,which tells a objective c compiler to include Foundation.h file before going to actual compilation.
  • The next line
        @interface SampleClass:NSObject
        shows how to create an interface. It inherits NSObject,which is the base class of all objects.
  • The next line
       - (void)sampleMethod;
       shows how to declare a method.
  • The next line
         @end
         mark the end of an interface.
  • The next Line
       @implementation SampleClass
        shows how to implement the interface SampleClass.
  •  The next Line
       -(void)sampleMethod{}
       shows the implementation of the SampleMethod.
  • The next Line
       @end
       marks the end of an implementation.
  • The next Line
        int main()
        is the main function where program execution begin.
  • The next Line
       /*......*/
       will be ignored by the compiler and it has been put to add additional comments in the program,            So such lines are called comments in the program.
  • The next Line
         NSLog(....)
          is another function available in Objective-c which causes the message "Hello guys" to be displayed on the screen.
  • The next Line
         return 0;
         terminates main() function and return the value 0.

Compile & Execute Objective-C Program:
Now when we compile and run the program,we will get the following result.

Hello guys  

No comments:

Powered by Blogger.