13

I am so confused between self and underscore to access the property in Objective c, whenever we create property, its getter-setter automatically generated. So we can access the same property with self.property and same as _property. In my opinion, there shoulb be some difference which i am not getting. PLease tell me with examples.

Tinku
  • 247
  • 1
  • 3
  • 13

3 Answers3

38

The underbar (underscore) version is the actual instance variable, and should not be referenced directly. You should always go via the property name, which will ensure that any getter/setter actions are honoured.

So if you code _property = 4, you have directly set the variable. If you code self.property = 4, you are effectively making the method call [self setProperty:4], which will go via the setter (which might do something such as enforce property having a max value of 3, or updating the UI to reflect the new value, for example).

Edit: I though it was worth mentioning that the setter (setProperty) will issue a _property = 4 internally to actually set the instance variable.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • 2
    Using property getter/setters also has the huge benefit of triggering key-value observers (KVO). – Cyrille Jun 17 '15 at 20:45
  • Cyrille - thanks. There may be other advantages too that I'm unaware of, but I'm pretty inexperienced with Obj-C. – Steve Ives Jun 17 '15 at 20:47
  • That's quite a precise answer from someone "inexperienced with Obj-C". Congrats. – Cyrille Jun 17 '15 at 20:50
  • Thanks - my general computer skill level is 'god-like' :-) I write operating system extensions in assembler for IBM mainframes. I remember when the concept of 'delegates' in Obj-C suddenly clicked and now they seem simple. – Steve Ives Jun 17 '15 at 20:58
  • Note also that setting a variable using the property syntax implies calling a method. Due to Objective-C runtime mechanisms, this could negatively affect performances when the value of the variable has to be set a high number of times. – SalvoC Jun 17 '15 at 21:00
  • @steve , can u more describe it? – Tinku Jun 17 '15 at 22:35
  • I am confusing in same class to access the property with self and underscore? – Tinku Jun 18 '15 at 04:51
  • 2
    Good answer, but I would add that there are certain circumstances in which directly accessing the instance variable is preferable to using the property: in initializers, dealloc, and custom accessor methods. Apple's Programming with Objective-C Guide has a whole section about this, [here](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html). – zpasternack Jun 18 '15 at 06:47
  • @SalvoC - Beware premature optimisation! If you have to (for example) loop and decrement a property from 50 to 1, you may think that 50 method calls will be slow and that you should just skip the setter and set the variable directly. However, whilst the method call might be slow compared to setting the variable 50 times, the difference might be a few micro- (milli-?) seconds, and if this is in response to someone clicking a button, the loop will be complete before their finger leaves the screen and the difference will have absolutely no effect, so it should be done the 'correct' way. – Steve Ives Dec 30 '15 at 10:12
  • @Steve Ives - the question was about the difference between the two methods, what should we be aware of? – SalvoC Feb 03 '16 at 15:44
  • @SalvoC - if you use underscroe, you are accessng the variable directly and bypassing any protection the setters and getters may have implemented. If you use self, you are setting the variable the correct way (apart from some specific exceptions) via the property's setters and getters. You would have to set the variable via the property an exceedingly high number of times to cause a performance issue. If this is the case, you are probably doing it wrong. Bear in mind that you have to use _ (underscore) within the setter and getter methods, for obvious reasons. – Steve Ives Feb 03 '16 at 15:51
11

when you are using the self.XX, you access the property via the setter or getter.

when you are using the _XX, you access the property directly skip the setter or getter.

Joyal Clifford
  • 1,212
  • 11
  • 20
6

Let's say you have a property defined as follows:

@property (nonatomic,strong) NSString* name;

The getters and setters of the name property are automatically generated for you.Now, the difference between using underscore and self is that:

self.name =@"someName"; // this uses a setter method generated for you.
_name = @"someName"; // this accesses the name property directly.

The same applies for getting the name property;

Lehlohonolo_Isaac
  • 1,426
  • 13
  • 15