Thanks
Thanks

Reputation: 40359

What's the difference between @property and @synthesize?

Like I understand, @synthesize actually is generating the Getters and Setters. But what's @property then doing? Is it just setting up the parameters for that cool @synthesize magic function?

Upvotes: 4

Views: 3725

Answers (4)

michaelemery
michaelemery

Reputation: 111

There is a common misconception that the @synthesize directive is required in order to implement setters and getters created with the @property directive, but this is not the case. Using the @property directive without @synthesize will still generate setters/getters and allow you to use dot notation. However, leaving out the @synthesize directive will cause the compiler to generate corresponding instance variables with a leading underscore character, e.g. the property myVar will have an instance variable of _myVar.

Using a leading underscore is a common convention that allows you to differentiate between properties and instance variables. It is also common for properties and instance variables to share the same name, which is what @synthesize does by default.

Upvotes: 1

mouviciel
mouviciel

Reputation: 67919

@property declares getter and/or setter

@synthesize implements them.

Upvotes: 4

pgb
pgb

Reputation: 25011

@property declares the name as a property. This means, it will be accessible via the dot syntax (object.value).

@synthetize can be seen as a macro, that creates the getter and setter methods. It is useful to know that you can override those methods, even if you type have the @synthetize in place.

Upvotes: 10

oxigen
oxigen

Reputation: 6263

You write @property in header file

@property float value;

is equivalent to:

- (float)value; 
- (void)setValue:(float)newValue; 

It get information for OTHER classes, that your class has this methods

@synthesize phisicaly CREATE these methods in class implementation

Upvotes: 9

Related Questions