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!
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!
The Objective-C Programming Language: 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.Just a quick example of why you might not want to do just "variable = 0":
Say you have this property:
@property (nonatomic, retain) id <MyDelegate> theDelegate;
Whenever you replace that delegate with a new one, your synthesized setters and getters will handle the release/retain for you every time you set it like so:
self.theDelegate = newObject;
Really what happened was this:
[self setTheDelegate:newObject];
- (void)setTheDelegate:(id <MyDelegate>)anObject {
[theDelegate release];
theDelegate = [anObject retain];
}
(This is simplified of course)
You can do very powerful things in your own setters and getters, synthesize is for those that happen over and over like retained properties, etc. When compiling it looks at your @property and builds the methods accordingly.
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).