Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 911
esqew Avatar asked Jul 03 '10 00:07

esqew


2 Answers

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.
like image 151
Franci Penov Avatar answered Oct 28 '22 09:10

Franci Penov


The "@" symbol is interpreted by the compiler as a directive. This is one of the Objective-C 'additions' to the C language. When you declare @property and then @synthesize you are instructing the compiler to create the instructions and corresponding symbols for getters and setters for you. Remember that in the C language, the "=" operator means "assign". Most of the time in the OO context that the Objective-C extensions provide, we are working with pointers (aka references) to isa data structures (Classes in Objective-C).

Prior to Objective-C 2.0, all of the getter and setter methods had to be coded by the developer for every attribute which for most cases was copy/paste code. To be completely KVC/KVO compliant requires a lot of very tedious code... willAccessValueForKey, didUpdateValueForKey statements etc. that the new compiler adds for you automatically when you use the @property/@synthesize syntax. This is a huge productivity boost for developers. The dot syntax additions to the language are a little more contentious in the community as this hides the magic the compiler is doing on you behalf to interpret the object.property = anotherObject.property; statement as [object setProperty:[anotherObject property]];

From the Apple documentation referenced in other answers

Property Declaration Attributes

You can decorate a property with attributes by using the form @property(attribute [, attribute2, ...]). Like methods, properties are scoped to their enclosing interface declaration. For property declarations that use a comma delimited list of variable names, the property attributes apply to all of the named properties.

If you use the @synthesize directive to tell the compiler to create the accessor method(s), the code it generates matches the specification given by the keywords. If you implement the accessor method(s) yourself, you should ensure that it matches the specification (for example, if you specify copy you must make sure that you do copy the input value in the setter method).

like image 45
falconcreek Avatar answered Oct 28 '22 10:10

falconcreek