×

Objective-C Basic Syntax



#import <Foundation/Foundation.h>

@interface DemoClass:NSObject
- (void) displayMethod;
@end

@implementation DemoClass

- (void)displayMethod{
   
NSLog(@"Welcome to Objective-C ");
}

@end

int main()
{
  
   DemoClass *demoClass = [[DemoClass alloc]init];
   [demoClass displayMethod];
   return 0;
}

 

Output :-

 Welcome to Objective-C

Main Building Blocks of Objective-C Program

1) Preprocessor Commands

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.

2) Interface Section

The next line @interface SampleClass:NSObject shows how to create an interface. It inherits NSObject, which is the base class of all objects.

3)Methods

(void)diplayMethod; shows how to declare a method.

4) @end

The next line @end marks the end of an interface.

5) Implementation Section

The next line @implementation DemoClass shows how to implement the interface DemoClass.

6)Main Function

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

7)NSlog

NSLog(...) is another function available in Objective-C which causes the message "Welcome to Objective-C!" to be displayed on the screen.