RSS

Basics of Objective C

08 Aug

In this post we shall discuss the basics syntax of objective c, to know more on objective c you may refer this post which has been posted earlier.

Before learning any particular programming language you must be familiar with it’s syntax so that you can get a clear hold of that particular  language, so given below are the syntax of objective c kindly go through it very nicely as you will be using these syntax throughout your iOS development career. Also an advice from my end don’t compare this language with any other programming language because its quite different from other programming language.

Class Declaration: Let’s have a look at the class declaration part of objective C, in objective c their are two files called as the .h which deals with declaration part of your program in this file you will only declare the functions and global variables nothing else and the other is called as the .m file which takes care of the implementation part of all the methods that you have declared in the .h file of your program. So you can easily say that in objective c their are two files one is the .h file and the other is the .m file and together these files make a single class. Now lets have a look at these files.

Declaration inside the .h file

 

 

 

 

 

 

 

 

 

 

The foundation framework contains the data about all the datatypes in objective c and hence it is imported their. The name of our class is calculator which is inheriting another class called as the NSObject which is responsible for allocation memory for your class with the help of two important method alloc and init (which you shall see later). Now let’s have a look at the .m part

The above code is inside the .m file where you will have to implement all the methods that you have declared inside the .h file

Method Declaration:

Their are two types of methods one is a static or class method and the other is non-static or instance method, the declaration of these methods are also different whose syntax is discussed below

Static method declaration: The static method is declared with a plus sign in front with a return type written in c style bracket followed by the method name

 

 

 

 

 

 

 

 

 

 

 

 

 

Inside the .m part

Non-Static Method or Instance method: The non-static method is declared with a minus sign in front with a return type written in c style bracket followed by the method name.

 

 

 

 

 

 

 

 

 

 

 

 

 

Inside the .m file

Function with parameter: The syntax of functions with parameter is somewhat different in objective c as compared to all the other languages so just have a look at the image given below with the explanation.

In the above image you can see the function with the parameters which is used to find the volume of rectangle, and given below is the implementation part for this method.


Object Creation in Objective C: Finally when your class is ready you need to create the object of your class to make a call to the methods that you have created, so given below is the syntax of object creation in objective c which should be written inside the main method.

Before creating the object inside the main method it is necessary that you import the calculator.h file into your project so that the pre-processor will come to know about the location of this particular file into your project, it is just like when you use pow function in c programming you include the math.h library into your application so that the reference for the pow function and all the other math related function is provided into your application.

Inside the main method their are hell lot of stuff which will be discussed later in the memory management section but for now lets focus on the object creation part, the name of the object of the calculator class is calculatorObject and here you can see that their is a function called as the alloc which is used to allocate a memory block for your object and init is like your constructor which will initialize the memory block allocated by alloc.

I hope that you have understood how to declare a class, function and object in objective c, you can get the copy of this post as a PDF here. If you are having any queries then do let me know via comments or you can email it to me at iphone.learning@quagnitia.com, until then happy iCoding and have a great day.

Advertisements
 
7 Comments

Posted by on August 8, 2011 in Beginners

 

Tags: , ,

7 responses to “Basics of Objective C

  1. Mangesh

    August 8, 2011 at 11:31 am

    Great work!an absolute start for begineers

     
  2. suraj shinde

    August 25, 2011 at 6:00 am

    very simple notes i seen uptill now

     
  3. dinu Bhau

    August 25, 2011 at 6:04 am

    it is an very nice for which are unkown about objective c concept

     
  4. Vinod Shimpi

    August 25, 2011 at 6:05 am

    Good Job it was Use Full for iPhone Beginner’s and Who want to Learn Objective C.
    I Specially Refer to iPhone Beginners to Read This.

     
  5. suraj shinde

    August 25, 2011 at 6:05 am

    self study ke liye best notes

     
  6. Onkar Karoshi

    August 25, 2011 at 6:15 am

    nice one. very simple to learn.
    plz post on next topics…

     
  7. Suraj R. Mirajkar

    September 12, 2011 at 8:57 am

    @interface MyClass : NSObject

    -(void)myInstanceMethod;
    +(void)myStaticMethod;

    @end

    Instance Methods can be called using its object while Static Methods can be called using its Class name.

    Means :-

    If we want to call “-(void)myInstanceMethod;” method we can do it as below :

    MyClass *ObjofMyClass = [[MyClass alloc]init];
    [ObjofMyClass myInstanceMethod];

    And when we want to call “+(void)myStaticMethod;” method we can do it as below :

    [MyClass myStaticMethod];

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: