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
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
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 email@example.com, until then happy iCoding and have a great day.