Search code examples
iphoneios4objective-c-2.0

What is the point of @property and @synthesize?


I haven't been able to figure it out, and there are no websites which explain it clearly enough... what exactly are the purposes of @property and @synthesize?

Thanks in advance!


Solution

  • Objective-C Runtime Programming Guide: Declared Properties

    @property declares the getter and the setter methods for the public property you want to implement. For example this property declaration:

    @property float value;
    

    is equivalent to:

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

    @synthesize provides default implementation for these two accessors.

    Update: The above explains what these two do. It does not explain what their purpose is. :-)

    • @property adds a member to the public interface that acts as a data variable to your class clients, but is read and written using methods. This gives you better control over the data that is exchanged between the client and your code, for example you can do extended validation on the values your code is given.
    • @synthesize allows you to not explicitly write the code that will be called by the client and actually treat the property as a data variable yourself.