Declaring a property in Objective-C

Possible duplicate:
How is the underscore in front of a variable in the cocoa objective-c class?

So, I figured out that you should use underscore when synthesizing properties, and that doesn't make the least sense to me.

So let's get started. In our .h file, write this line:

@property (nonatomic) double speed; 

In our .m file, we do the following:

 @synthesize speed = _speed; 

Why? As far as I know, the property creates an instance variable and creates setters and getters for it. But what the hell line

 @synthesize speed = _speed 

make? As common sense tells me, we assign the speed value _speed to speed. Good. Where did we declare _speed? Why does the compiler not give us an error? What does it mean? Why such confusing code?

My questions are here:

What happens if I just

 @synthesize speed; 

without _speed, will I get an error or some errors? What is the reason for this syntax? What did they think when they did this? Where does _speed come from? What is it? Is it a pointer or a real value? What's happening?

+4
source share
8 answers

Well, _speed is the instance variable used by this property. If you really want to declare it completely, you need to write:

 @interface Foo { double _speed; } @property double speed; @end @implementation Foo @synthetize speed = _speed; @end 

Xcode 4.4 is now untied, most of this material is unnecessary and should be omitted for convenience.

Basically, all you have to do is:

 @interface Foo @property double speed; @end 

There is no instance variable, no @synthetize , and everything is still working as before. You can use self.speed or self->_speed for direct access.

+5
source

ok, you don't need to do this ...

However, by doing this, you cannot use the name of the unstressed name of this property, because in fact you will mix with access to iVar directly, and then through the property.

In other words, by doing this:

 @synthesize myProperty; 

In your code, you can refer to it as myProperty ( iVar ) or self.myProperty (property - with access rules - readonly / readwrite, getter, setter, etc.), this can, of course, be confusing if you use such rules, as:

 @property(nonatomic, retain, getter = anotherProperty) NSNumber myProperty 

By accessing this with myProperty in code, you expect to get anotherProperty

This is only possible if:

 self.myProperty 

So, in Objective-C, we can do the following to avoid this:

 @synthesize myProperty = _myProperty; 

Therefore, in the code, the error actually refers directly to myProperty , instead you should do self.myProperty (access to properties) or _myProperty (access iVar).

+1
source

You do not need to do this. This is a common practice. Speed ​​... = _ will create a property named _speed, and its setter and getter will be named after speed. This allows you to distinguish between

 self.speed = value; 

and

 speed = value; 

because the latter will create a compiler error.

 _speed = value; 

will be correct in this case.

This helps to avoid saving / releasing errors, since the installer will save the object, and simple assigners will not. (self.speed = ... will call the setter!)

If you omit "= _speed" in the synthesizer instruction, the property will only be called "speed". If you are not mistaken, this will work perfectly.

+1
source

This is a naming convention; you do not need to do this. People tend to do this so that they can distinguish between an instance variable and a property.

Usually he goes

 .h @interface myClass : NSObject { NSNumber *_speed } @property (nonatomic, strong) NSNumber *speed; .m @implementation @syntesize speed = _speed; 
0
source

Nothing bad will happen - it's just to just write @synthesize speed. Using _speed just makes the _speed instance variable instead of speed. Most people do this to avoid accidentally accessing the ivar directly, rather than through a cable.

0
source

From the documentation

@synthesize speed = _speed; will point the speed property to the _speed instance _speed .

You do not have to do this. @synthesize speed working fine.

0
source

In a manual memory management environment:

Why do I need an instance variable name at all?

  • You need to free memory that can be used by your properties in dealloc
  • You can use self.field = nil , but this approach can cause problems
  • So, you need an instance variable that is used behind this property so that you can [_field release]; in dealloc.

The same if you need to access a property in the initializer.

Why do I need an underscore in the instance variable name?

  • To never accidentally use ivar directly and break the memory management contract for a property

 @property (retain) UILabel *label; ... @synthesize label; ... -(void)viewDidLoad { [super viewDidLoad]; label = [[[UILabel alloc] initWithFrame:frame] autorelease]; } 

here by accidentally losing yourself. you have created a label potential "dangling pointer". If you used the code @synthesize label = _label; above, this would create a compiler error.

  • Local variables or method parameter names often have the same meaning as the property name - if you use the underscored instance variable, this will not cause any problems.
0
source

It will be fine, why don't you try?

-1
source

All Articles