3

Possible Duplicate:
difference between accessing a property via “propertyname” versus “self.propertyname” in objective-c?

In my class, i am not sure whether to use self.property of property, such as

  dateFormatter = [[NSDateFormatter alloc] init];
  //or
  self.dateFormatter = [[NSDateFormatter alloc] init];

They both seem to work fine. Are there any differences between these two usage? I am confused.

Community
  • 1
  • 1
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • 1
    Possible Duplicate [http://stackoverflow.com/questions/5251600/difference-between-accessing-a-property-via-propertyname-versus-self-property](http://stackoverflow.com/questions/5251600/difference-between-accessing-a-property-via-propertyname-versus-self-property) – Malek_Jundi May 05 '12 at 12:38
  • Thx, actually i have another confusion between property and _property, could help me out for this question? – lu yuan May 05 '12 at 12:53

2 Answers2

3

property is inface your methods getter and setter, when ever you call it via self.dateformator, it will call the property if you have synthesized the object and self generated getter and setter naming(setProperty and getProperty),

in your case, your first line is not the propery, you are accessing directly the iVar of your class while in 2nd line of your code you are using the property getter and setter methods,

now your question what is the difference,

the difference is that in iVar access(your first line), you will have to manually release the object and will have the retain count of 1 increased, and memory allocted will be accociated to it. while in self.property, a memory is allocated, but a new block will be assign to the variable as apple property management do this. but the retain count will be again the same.

so a block of memory will be lost.

Now i would like to tell some thing beneficial that is the good approach is to use properties for object, because if you have written retain in the property attributes in interface file, then your memory management will be at the compileres end, but remember to write release in dealloc method. and for code lines that you have writter here, like

self.someProperty = [[NSArray alloc] init];

use it as

NSArray* arr = [[NSArray alloc] init];
 self.someProperty = arr;
 [arr release];

now your retain count will be same as you want that of one, and don'd care of where to release this, it will auto release in dealloc method if you writtern it.

and for the one you write earlier, you will have to keep track that where you have to release the object

Saad
  • 8,857
  • 2
  • 41
  • 51
  • However with ARC, automatic reference counting, since XCode 4.2, you do not need to worry about releasing your objects any more ;) – rajomato May 05 '12 at 23:44
  • yeah it is good step but i thnk in starting one year, you should not use ARC, just for knowlge abt memory management – Saad May 06 '12 at 06:00
2

You could actually use self.property if you want to use its setter and getter functions. That's the main function of properties, they make setter and getter methods for you without you having to code them yourself. Now if you leave away the sel. and just use your property, then you are not accessing any of its getter oder setter methods. But that does not matter in your case since allocating and initializing an object does not have any use of the steer or getter methods. That is why both ways are working for you.

'self.property' accesses the setter and getter methods of the property.

'property' accesses the instance variable of the property.

Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
rajomato
  • 1,167
  • 2
  • 10
  • 25