3

I have seen code like that in the Application delegate in iPhone project for example.

what is the variable with the underscore means? can I use it as setter and getter for the variable?

also when releasing the variable should I use:

  [variable release]; 

or

 [_variable release]; 

Thanks.

Samssom
  • 55
  • 6
  • 1
    possible duplicate of [Prefixing property names with an underscore in Objective C](http://stackoverflow.com/questions/3521254/prefixing-property-names-with-an-underscore-in-objective-c) and [How does an underscore in front of a variable work](http://stackoverflow.com/questions/822487/) and [Underscore prefix on property name](http://stackoverflow.com/questions/5582448/underscore-prefix-on-property-name) and [Why do you use an underscore for an instance variable but not its corresponding property?](http://stackoverflow.com/questions/2371489/) – jscs Jun 29 '11 at 19:21
  • none of them mentioned which variable to release! – Samssom Jun 29 '11 at 19:39

3 Answers3

5

In some coding conventions the underscore before instance variables is used to be able to quickly differentiate them from other variables. It also helps avoid naming conflicts with local variables in methods and subclass methods.

@synthesize variable = _variable 

Creates a setter and getter that set/get the variable you set it to in this case _variable. So outside access uses code like object.variable which is really just returning _variable. however the class usually uses the _variable internally.

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • This is an old post, but I'd like to clarify. Does doing this create the setter and getter, so just using object.variable anywhere will behave as it should? Or do I still need to define a setter and getter in addition to this? – muttley91 Jun 10 '13 at 04:59
  • @rar if you `@property` and `@synthesize` the setter and getter are created for you. You can access object.variable just fine after that. – Justin Meiners Jun 10 '13 at 05:03
  • Wow, I wrote a bunch of unnecessary code then (because I wrote out the getters). Thanks! – muttley91 Jun 10 '13 at 05:04
  • @rar it is especially useful for retained properties - lots of code you don't want to write :) – Justin Meiners Jun 10 '13 at 05:10
3
@synthesize variable = _variable;

The property name is "variable" and the instance variable that backs it up is named "_variable". You should use the accessors -variable and -setVariable: rather than accessing the ivar directly, except in -init and -dealloc, where you'd use _variable.

Caleb
  • 124,013
  • 19
  • 183
  • 272
1

In your example variable is a property and _variable is an instance variable. For simplicity sake we can say that by synthesizing you are essentially instructing that the property ( in our case variable) will use the instance variable ( in our case _variable) for storing and retrieving values. What you are really doing is instructing the compiler to create implementations that match the specification given in the property declaration.

The suggested way of releasing when you are using a property will be to just assign it nil. This would essentially release the object and also set the instance variable to nil instead of being a dangling pointer.

If you were not using property then you can call the release on the instance variable and then ideally you want to set it to nil.

Vidyanand
  • 967
  • 8
  • 14