What do @synthesize
and @property
do in Xcode? Please provide an explanation in really simple terms?

- 12,167
- 20
- 88
- 148

- 7,277
- 15
- 55
- 100
6 Answers
You asked for simple terms:
@property declares a property in your class header
@property (nonatomic, retain) NSString *myString;
@synthesize creates your setter and getter for your property (accessor methods)
Without synthesize you have to write your own setter and getter implemention, like getMyString or setMyString (capitalize the first character of your property)
Sam: Just an advice: http://www.cocoadevcentral.com/d/learn_objectivec/ is a pretty solid resource to learn about basics like properties.
Good Luck!

- 36,243
- 17
- 80
- 100
-
yea, but my application runs fine without it. so, why are they needed? – Sam Jarman Jan 09 '10 at 08:45
-
They're not *needed* they're just useful for saving time and avoiding mistakes. – Tom Duckering Jan 09 '10 at 08:46
-
On the desktop you can use manual memory management, or you can use the garbage collector. Manual management works fine, but it's useful, time saving and less error to use Garbage Collection. – Abizern Jun 03 '11 at 01:16
Properties and synthesized accessors are new features in Objective-C 2.0.
When you declare a @property
you declare somewhat an instance var. Then you @synthesize
accessor methods (i.e. getter and setter) for that property.
There are also @dynamic
accessors if you're interested.
You should really do your homework on this. Apple has nifty pdf for that.

- 48,927
- 17
- 132
- 168
-
-
5I googled that for you: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf – Eimantas Jan 09 '10 at 08:57
-
1I should add that with the modern runtime (as supplied with iOS and 64-bit Cocoa) if you use @property and @synthesize, you don't need to declared the backing iVars in the interface. Which means less duplicated code. – Abizern Jun 03 '11 at 01:17
-
1@Eimantas, the PDF you link is the basic Obj-C manual. Not specific to this question at all. – livingtech Aug 05 '11 at 18:24
-
@livingtech - it does explain everything about the properties. What's wrong about that? – Eimantas Aug 05 '11 at 18:44
-
4@Eimantas - If RTFM were a valid answer, it would be the answer to 90% of the questions on stack overflow. – livingtech Aug 08 '11 at 16:04
-
But seriously, you -1 my answer because of a link in a *comment* which backs up my answer? – Eimantas Aug 08 '11 at 16:50
Think of all objective-c magic as just a "smarter macro", like a "smarter #define statement" @property if you notice is always in the h file, @synthesize is always in the m file. So in the background @property (whatever) NSString *myString;
becomes a declaration of 2 methods and a private variable;
void set_myString:(NSString *) str;
(NSString*) get_myString;
declarations in the header file
to make them do something their implementation is added into m file when you type in @synthesize myString; which becomes something like void set_myString:(NSString *)str { myString = str; }
(NSString *) get_myString
{
return (myString);
}
But it's smarter than this depending on if you say "retain" "strong" or "weak" it will either just return the pointer to the myString or it will copy the myString into a new object
So all of this is done automatically by a compiler just by reading your declarations. Which is quite useful and saves a lot of time

- 629
- 13
- 9
it simply sets the property's setter variable name in it's own class.
For example lets say I have this: @property (nonatomic, copy) NSArray* viewControllers;
Well if i want to access the setter _viewController i wouldn't set a synthesize variable.
but if i want to access the viewController variable by the name viewController
instead of _viewController
, I would do @synthesize viewController;
.
If I wanted to use it as a completely different name I could do this @synthesize viewControllers = viewControlololer;
but that's only setter. As you will notice [self viewControllers]
only works and not [self viewControlololer];
So I don't understant why everyone writes sets the "setter and getter" of a property. It doesn't change the getter variable at all... unless that means [self viewController]
is aware of viewControlololer
(obviously).

- 12,854
- 5
- 62
- 89
Actually properties are synthesized, either implicitly or explicitly. Properties are implicitly synthesized. So there's no need to use synthesized unless you wanna change the name of the variable to something different than _property_name.
There are other use cases, for example if you don't want an instance variable to back your property.
Properties are explicitly synthesized by using the @synthesized directive.
(Answer extracted from the Big Nerd Ranch guide)

- 247
- 2
- 4
By default all our variables are Private so we can't acess out of the class.
if we want to use our instance variable in out of the class.
When you declare a @property
you declare somewhat an instance var. Then you @synthesize
accessor methods (i.e. getter and setter) for that property.
There are also @dynamic
accessors if you're interested.