views:

135

answers:

3

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!

+6  A: 

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.
Franci Penov
I don't know if I fully understood this... why not just do: value = 0.0;
esqew
@seanny94 See this question: http://stackoverflow.com/questions/1568091/why-use-getters-and-setters
Tobias Cohen
The answer is partially wrong. @property declares a getter (and/or setter). It also declares some aspects of behaviour of the getter/setter (e.g. retain, copy, assign, nonatomic). @synthesize provides a default implementation of the accessor(s) based on the @property. Neither of these allow you to treat the property as a data member any more than writing out standard method declarations and definitions does.
JeremyP
From the point of view of the client code that accesses the class, the property acts as a data member and has the semantic of a data member. And yes, the @property does declare some aspects of the behavior of the accessors, which I felt I can safely omit, since it's described in the full documentation I linked to.
Franci Penov
@Franci. No. Properties do not behave like data members. You do not access properties with the `->` operator from outside the class and you cannot omit `self.` / `self->` inside the class. Also, there is no functional difference between a property declared with `@property` or one declared by writing out the accessors in full.
JeremyP
Sure, if you want to be pedantic, there are certain syntactical differences between a property and a data member. After all they are two different language constructs (and the fact that Objective-C is poorly designed does not help :-)). Still, a property semantic from a client code's point of view is same as a data member - it's a value representation, not an action representation.
Franci Penov
A: 

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.

v01d
+1  A: 

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

falconcreek