The goal of C is to synthesize a property

Possible duplicate:
An underscore property name prefix in Objective-C

When synthesizing properties, I found that someone is doing:

@synthesize myVar = _myVar; 

what is "_myVar" and what is the difference: just

 @synthesize myVar; 

Finally, when did I prefer the first solution to the last?

Thanks Luca

0
source share
3 answers

What _myVar really is in your example is the name ivar that supports your property. By default, when you synthesize a property, the same name is created for you. Thus, you can use your property to set your ivar via setter / getter or _myVar to directly access your variable (bypassing KVC / KVO, of course).

EDIT: From Apple Coding Rules for Cocoa

... In many cases, when you use a declared property, you also synthesize the corresponding instance variable.

Verify that the instance variable name briefly describes the attribute saved. Usually you do not need to access instance variables directly, instead you should use access methods (you access instance variables directly in the init and dealloc methods). To help signal this, prefix instance variable names with an underscore (_) ...

+2
source

If you want to use any existing data element in setter and getter, then it can be specified as.

eg. @synthesize personName = pName;

by this we can use pName instead of personName in accordance with our convenience.

-one
source

This is the name of a private variable.

Answer to another question: answer

-one
source

All Articles