Saturday 1 November 2014

Objective C Code Building Blocks

Hello Friends,

Today I am going to show you how the Objective C programs can be written in a single file.

Before we study Advance building blocks of the Objective-C programming language, let us look a the minimum Objective-C program structure means We are going to create a full Objective Orientated Example with in a single file.

Basic Parts of the Objective C Programming Language:

  • Preprocessor Commands
  • Interface
  • Implementation
  • Method
  • Variables
  • Statements & Expressions
  • Comments


Create you Command Line Project under the Foundation and you will get the main.m file to code. Copy this following code into file.

#import <Foundation/Foundation.h>

/* First Section of .h file (Header ) */
------------------------------------------------------------------------------
@interface FirstClass:NSObject
- (void)showMethod;
@end
-----------------------------------------------------------------------------

/* Second Section of .m file (Implementation) */
@implementation FirstClass

- (void)showMethod{
   NSLog(@"This is the single file Example of Objective C! \n");
}

@end
----------------------------------------------------------------------------

/* Main method which starts the execution */
int main()
{
   /* Create an Instance of the FirstClass to access its method */
   FirstClass *firstClass = [[FirstClass alloc]init];

  /* Calling the showMethod using Class Instance */
   [firstClass showMethod];
   return 0;
}

Let's understand the above code structure


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. just like the stdio.h in C Programming language it's a basic library for Objective C language compilation.

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

3. The next line "- (void)showMethod;" shows how to declare a method. Here we have to just declare the method and after that we will implement it in other section.

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

5. The next line "@implementation FirstClass" shows how to implement the interface FirstClass.

6. The next line "- (void)showMethod{}" shows the implementation of the showMethod.

7. The next line "@end" marks the end of an implementation.

8. The next line "int main()" is the main function where program execution begins just like any other programming language.

9. 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 programs.

10. The next line "NSLog(...)" is another function available in Objective-C which causes the message "This is the single file Example of Objective C!" to be displayed on the screen.

11. The next line "return 0;" terminates main()function and returns the value 0.

I hope you guyes have understood this basic structure of the Objective C programming language. This language is devided into three parts according to the code.

1) Interface File also called the Header file with the extension of .h
2) Implementation file with the extension of .m
3) The main method with the extension of .m file

Video Tutorial




For More Detail you can find the lates information on below url.

http://programming-guru.com/
http://youtube.com/webboostings/
http://webboostings.blogspot.in/


Thank You
Abhishek Bendre

No comments:

Post a Comment