Tuesday, September 14, 2010

Properties in Objective C

In this blog i will discuss the following
  1. What is a Property?
  2. Why is it used?
  3. How to declare a property in Objective C?

Property: I will start from my first question and that's What is a Property?

Property is a value that is used for specific purpose although its use may differ from one program to another. With property you can assign a value and extract a value

Now why to use a property?

Well if your having a java or C# background then you must have read books which say that property is used for information hiding, i think that their are better ways of information hiding rather than using a property (My own view).

While using a property in java or in C# you used to write its getter and setter methods, well in Objective C its totally different.

Let's have a look at an example to see how property works in objective C.

In this example i am trying to add two numbers and the data will be coming from the property, just refer the sample code with comments that i have used to explain property

@interface Myclass : NSObject

{

int num1, num2;    //global variables of integer type

}

@property (readwrite,assign) int num1, num2; //set the property for those integer variable


//you use assign in a case where you cannot retain the value like BOOL or NSRect, else use retain


//If you use retain then you become the owner of that particular property and you must release that property once its use is over.


-(void)add;          //add function



@end


Implementation part of the class

@implementation Myclass        


@synthesize num1, num2;   //synthesize will automatically set getter and setter for your property


-(void)add

{

int result= num1+num2;


NSLog(@"Result of addition is %d",result);

}


@end

The final touch that's our main method

int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


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

[pool addObject:obj_Myclass]; //adding object to the pool

obj_Myclass.num1 = 10; //invoking setter method

obj_Myclass.num2 = 20; //invoking setter method

[obj_Myclass add];    //calling the add method

        [pool drain];

        return 0;

}



Their might also be a scenario where you want the property to be read only, well that again is very easy.
The below given code will make your property read only

@property (readonly,assign) int num1, num2;


You can change the value directly by referencing it directly, refer the code below


-(void)add

{

self->num1 = 10; //legal

self->num2 =20; //legal


int result= num1+num2;


NSLog(@"Result of addition is %d",result);

}


Well this was about the properties in Objective C, if i get to know any more details on properties i will surely update the blog regarding the same....

2 comments:

  1. Ravi,

    I really appreciate the efforts that you are putting in here.. This is cool and equally benevolent. Keep rocking !!!

    ReplyDelete
  2. thanks its good basics you describe.

    ReplyDelete