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:
#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:
is a preprocessor command,which tells a objective c compiler to include Foundation.h file before going to actual compilation.
@interface SampleClass:NSObjectObjective -C Hello World Example
A Objective-c program basically consist of the following parts:
- Preprocessor Commands
- Interface
- Implementation
- Method
- Variables
- Statements & Expressions
- Comments
#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:
- The first line of the program
is a preprocessor command,which tells a objective c compiler to include Foundation.h file before going to actual compilation.
- The next line
shows how to create an interface. It inherits NSObject,which is the base class of all objects.
- The next line
shows how to declare a method.
- The next line
mark the end of an interface.
- The next Line
shows how to implement the interface SampleClass.
- The next Line
shows the implementation of the SampleMethod.
- The next Line
marks the end of an implementation.
- The next Line
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
is another function available in Objective-c which causes the message "Hello guys" to be displayed on the screen.
- The next Line
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: