Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synthesized property and variable with underscore prefix: what does this mean? [duplicate]

Tags:

Possible Duplicate:
Underscore prefix on property name?

What does this mean? @synthesize window=_window; I know that in general it means that 'some class' has a window, but why use _window instead of just window? Is this a namespace thing?

like image 291
lampShade Avatar asked May 18 '11 18:05

lampShade


1 Answers

I'll give a go at describing this programming convention in basic English.

It is a very common convention in other languages to name member variables with a preceding m, m_, or _ to distinguish them from locally declared variables and to signify that they should have accessors written, if necessary (no classInstance.m_Variable = 5).

If an Objective-C programmer declares ivars following this convention (and they should) and uses the basic syntax @synthesize _window; then the usage for the property becomes somewhat ugly: classInstance._window = myWindow or [classInstance set_window:myWindow]. Using the syntax @synthesize window=_window; allows the Obj-C programmer to utilize a popular programming standard (preceding ivars with _) while simultaneously having property accessors that use the Apple standard classInstance.window = myWindow and [classInstance setWindow:myWindow].

like image 189
Thomson Comer Avatar answered Sep 20 '22 13:09

Thomson Comer