Wednesday 5 June 2013

Introduction to Objective C and its Syntax


Hi Guys, hope all of you are doing good. In this tutorial, I am planning to discuss something about Objective C and how it differs from other programming languages. I am writing this post to help beginners who are learning Objective C to develop Mac OS X or iOS applications. Prior knowledge of OOPS will help to understand Objective C better. Apple have used Objective C for their OS X, iOS and to develop their frameworks COCOA(MAC) and COCOA Touch(iOS) frame works.

Object Oriented Language :

Objective C is Object Oriented C. C is a structured language. We can't create classes and objects in C. Where as in Objective C we can create classes, objects etc how other object oriented languages are doing. Objective C is super set of C. We can compile any C program using a Objective C complier and we can write any C code inside a Objective C class.

Message Passing :

Objective C uses message passing instead of method calling  which is different from other languages like C++, Java etc. Message passing means, the target of the message is resolved during run time. System resolves during run time whether the receiver responds to the method or not. This helps in attaining dynamic binding. In Objective C, if you send a message to nil, it will simply discard the message and nothing happens. Where as in Java, you will get a run time exception.

Syntax :

Objective C syntax differs from Java, C++ and other Object Oriented languages. It follows Small talk kind of syntax. When comes to using primitive data types(int, float, char etc) it follows C syntax.

Lets suppose there is a method called firstName in class Employee. Following code snippet shows how Objective C syntax looks like to send setFirstName message to object emp of class Employee :

Objective C :

// Method Syntax
- (NSString *)firstName
{
return firstName;
}

// Message Passing
[emp firstName];

Java :

// Method Syntax
String firstName()
{
return firstName;
}

// Method Calling
emp.firstName();

C++ :

// Method Syntax
string firstName
{
return firstName;
}

// Method Calling
emp->firstName();

Now, take a look at syntax of method having single argument. Lets say, setSalary with int argument.

Objective C :

// Method Syntax
- (void)setSalary:(int)aSalary
{
}

//  Message Passing
[emp setSalary:10000];

Java :

// Method Syntax
void setSalary()
{
}

// Method Calling
emp. setSalary(1000);

 C++ :

// Method Syntax
void setSalary()
{
}

// Method Calling
emp-> setSalary(1000);

Now, take a look at syntax of method having multiple arguments. For method to set firstName and lastName of employee :

Objective C :

// Method Syntax

- (void)setFirstName:(NSString *)fName andLastName:(NSString *)lName
{
}

// Message passing
[emp setFirstName:fName andLastName:lName];

Java :

// Method Syntax
void setFirstNameAndLastName(String fName, String lName)
{
}

// Method Calling
emp. setFirstNameAndLastName(fName,lName);

C++ :

// Method Syntax
void setFirstNameAndLastName(string fName, string lName)
{
}

// Method Calling
emp->firstName(fName,lName);

Interfaces and implementations

In Objective C, each class has two sections. One is interface and other one is implementation. As per coding conventions, interface will be placed in "Header(.h)" file and implementation placed in "Implementation(.m)" file.

We will continue with Employee class. Interface for an Employee class will look like below:


Interface :

@interface Employee
{
         // Instance variables
         NSString *firstName;
         NSString *lastName:
}

// Instance Methods
- (NSString *)firstName;
- (NSString *)lastName;

- (void)setFirstName:(NSString *)firstName;
- (void)setLastName:(NSString *)lastName;

// Class Methods
+ (int)numberOfEmployees;

@end

Implementation :

@implementation Employee

// Instance Methods
- (NSString *)firstName
{

}

- (NSString *)lastName
{

}

- (void)setFirstName:(NSString *)firstName
{

}

- (void)setLastName:(NSString *)lastName
{

}

// Class Methods
+ (int)numberOfEmployees
{

}


@end

NSObject is the root class of all Objective C classes. Each class of Objective C is inherited from root class NSObject.

Thats all for now. I hope you guys got good understanding about Objective C. Please feel free to add your comments and suggestions.

In our next topic, we will cover constructors and creating objects in Objective C.

3 comments: