Objective-C Hello World Example

Objective-C Program Structure

An Objective-C program fundamentally comprises of the accompanying parts: 

1. Preprocessor Commands 

2. Interface 

3. Usage 

4. Technique 

5. Variables 

6. Explanations & Expressions 

7. Remarks 

Give us a chance to take a gander at a basic code that would print the words "Hi 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;
}

_______________________________________________________


Give us a chance to look different parts of the above system: 1. The primary line of the system #import <Foundation/Foundation.h> is a preprocessor charge, which tells an Objective-C compiler to incorporate Foundation.h record before going to genuine aggregation. 2. The following line @interface SampleClass:NSObject demonstrates to make an interface. It acquires NSObject, which is the base class of all articles. 3. The following line - (void)sampleMethod; demonstrates to pronounce a strategy. 4. The following line @end marks the end of an interface. 5. The following line @implementation SampleClass demonstrates to execute the interface SampleClass. 6. The following line - (void)sampleMethod{} demonstrates the execution of the sampleMethod. 7. The following line @end marks the end of an execution. 8. The following line int principle() is the primary capacity where program execution starts. 9. The following line/*...*/ will be disregarded by the compiler and it has been put to include extra remarks in the system. So such lines are called remarks in the system. 10. The following line NSLog(...) is another capacity accessible in Objective-C which causes the message "Hi, World!" to be shown on the screen. 11. The following line give back 0; ends main()function and returns the quality 0.

Compile & Execute Objective-C Program:

Presently when we assemble and run the project, we will get the accompanying result.

2015-02-02 22:38:27.932 demo[28001] Hello, World!





0 comments:

Post a Comment

My Instagram