Monday, 5 December 2016

Friendship with Objective - C Syntax

Hello Friends,

In my previous post on OOP Concepts, I have explained the most widely used features of OOPs with 
real time examples to easily co-relate the programming in our daily life scenarios.

Also I have briefly explained about Objective - C overview and Why to choose iOS developer as a career, in my initial posts. Now as you are clear about the purpose of learning iOS development, from here onwards we will be discussing about iOS Development using Objective - C and Swift languages.

So lets start our journey by making some new friends who will be there till the end of our iOS development. These new friends are nothing but the Syntax of Objective - C and Swift.
Today we will be exploring Objective - C syntax.

Before showing you the syntaxes used in objective c , lets see few important things :


  • Objective C is case sensitive language. It means myVariable and MyVariable will be considered as two variables .
  • Every line in objective c ends with semicolon ( ; ).
  • There are some reserved keywords which we can't use as variable or constant names in our program.Below is the list of those keywords. So avoid to use them as the compile will complain about their use.



Fig. Objective - C reserved Keywords

1.Variables and constants 

Variables:

datatype variableName = valueToassign;
ex.  int myNumber = 123;

In the above statement, int is the datatype of the myNumber variable and at the same time it is being initialised with the value of 123;


Constants 

There are two ways to declare a constant:

1. Using const keyword

const datatype variableName = valueToassign;

ex. const int myConstant = 12;

Here  const keywords indicated that myConstant is a constant and has a value of 12. We can't change value of a constant.

2. Using #define

#define identifier value
ex. #define  PI  3.147;


2. Methods or Function 

A function is nothing but the set of statements to perform a specific task. We normally divide our program into functions for code readability and reusability. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.In Objective-C, we call the function as method.
- (return_type) method_name:( argumentType1 )argumentName1 
joiningArgument2:( argumentType2 )argumentName2 ... 
joiningArgument_n:( argumentType_n )argumentName_n 
{
   body of the function
}

For ex. 

- (void) callPerson (Nsstring*) personaName   withPhoneNumber : (Nsstring*) phoneNumber
{
 NSLog(@"Write method implementation here..!!");
}



How to call a method ?

-(int) [personObject callPerson:@"Vishal" withPhoneNumber: @"9876543210"];


3. Class and object 

Class declaration


@interface MyClass:NSObject 
-(void)MyMethod
@end

Class Implementation

@implementation MyClass
- (void)MyMethod
{
   body of the function
}

Object declaration

MyClass * myClassObject = [[MyClass alloc]init];                           

NOTE: Don't be scared about words like alloc , init, @interface,@implementation here. These are only keywords and we are going to see them i detail in coming posts. :) 


I have not included other syntaxes here like syntax for loop statements , enums etc. as we are going to explore theme in our iOS journey. My motto for this post was just to introduce you with the syntax of Objective C and I strongly believe that to start any coding, the above three syntaxes are enough.

For today that's it. We will write our first class in the next post and will make our hands dirty here onwards. 

Happy Coding...!!! 



Share:

0 comments:

Post a Comment