Do not use dot syntax to access you property in your custom setters.

Let’s have the following scenario:

You have class named Salary and you have an instance variable HRA. And you write a custom setter for the HRA variable.

-(void)setHRA:(float)newHRA{

newHRA = Some custom calculation;

self.HRA = newHRA;

}

If you do this you will run in to an infinite loop, because you call the setter method of HRA within the same setter method’s implementation.

This is equivalent to the following:

-(void)setHRA:(float)newHRA{

newHRA = Some custom calculation;

[self  setHRA:newHRA];//Infinite loop

}

So don’t use self in your setter implementations to access the instance variable.

Advertisements