Question

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?

Was it helpful?

Solution

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

OTHER TIPS

@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.

@property declares getter and/or setter

@synthesize implements them.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top