Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?
So i have found out that you have to use underscore when synthesizing properties and that doesn’t make a single bit of sense to me.
So, let’s start.
In our .h file we write this line:
@property (nonatomic) double speed;
In our .m file we do this:
@synthesize speed = _speed;
Why? As far as I know, property makes an instance variable and creates setters and getters for it.
But what the hell does line
@synthesize speed = _speed
do?
As common sense tells me, we assign value in _speed to speed. Okay.
Where did we declare _speed? Why doesn’t compiler give us an error? What is it supposed to mean? Why such an obfuscated code?
My questions here:
What happens if I do just
@synthesize speed;
without _speed, will I get error or some bugs?
What is the reason after this syntax? What were they thinking when making it? Where does _speed come from? What is it? Is it a pointer or a real value? What is going on?
Well,
_speedis the instance variable used by the property. If you really want to declare it fully, you need to write:Now Xcode 4.4 is unleashed, most of that stuff is unnecessary and should be omitted for sanity.
Basically, all you have to do is:
No instance variable, no
@synthetize, and everything still works as before. You can either useself.speedorself->_speedfor direct access.