Saturday, April 14, 2012

Programming language for iPhone

If you are working on some application then you need a programming language, the programming language that you will be using to build applications for iPhone is Objective C, Objective C is object oriented programming in C the editor used for developing application for iPhone is Xcode. 

Objective C follows the object oriented syntax from Small Talk and non object oriented syntax from ANSI C. It was invented by Brad Cox and Tom Love in the organization named Step Stone. In today's world where lots of developers are familiar with programming languages such as JAVA, Cpp, C#, VB.NET, objective c will be quite new to them, don't worry its not that hard once if you get the syntax of objective C you will be developing applications for iPhone in no time. So lets have a look at the syntax part of objective C and one more thing since objective C is a super set of C so any C program which you write in Objective C will run smoothly. 

Class Syntax: 

Class is like a blue print of a building with the help of which you can construct a building. In programming terms class is said to be as a blue print for your objects. Given below is the syntax to declare a class in Objective C. 

@interface <#class#> : <#superclass#> 

<#ivars#> //global variable declaration 


<#methods#> 

@end 

Example: 

@interface Myclass :NSObject 

int num; //global variable 

-(void)fun; // object method 

@end 

In the above example you can see that Myclass is inheriting from a class called NSObject, NSObject contains two method called alloc and init with the help of which you can create objects for your class. If you don't inherit your class from NSObject then you cannot create objects for your class. alloc will allocate memory for your class and init will initialize your data member and data functions in that allocated memory. 

In the above code we have seen only the declaration of our class but what about the implementation part??? 

In order to implement the above class objective C uses a keyword called as implementation have a look at the given example below: 

Example: 

@implementation Myclass 

-(void)fun 

printf("hello"); 


@end 


Functions: Functions are just a block of code which will do certain task for you as per your code written in them. Now functions are of two types: 

  1. Class functions or static functions.
  2. Object functions.
Syntax for class functions or static functions is given below: 

+(void) displaydata; 

Syntax for object function is given below: 

-(void) displaydata; 

The + sign is used to make a class level method (static method) and – sign is used to make an object method. 

Now lets integrate all the things which we have learned so far in an example 

Example: 

@interface Myclass : NSObject 



+(void)classmethod; //static method 
-(void)objectmethod; //object method 

@end 

@implementation Myclass 

+(void)classmethod 

printf("You are in class method\n"); 

-(void)objectmethod 

printf("You are in object method"); 


@end



Object creation: Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples
of real-world objects: your dog, your desk, your television set.

Syntax of object creation:

Myclass *obj = [[Myclass alloc]init];

alloc will create a new instance of the class and init will initialize the class level variables and methods. 

No comments:

Post a Comment