I was wondering what the point of @property and @synthesise were. At the moment I use the following to declare something:
//Class.m
#import "Class.h"
CCNode *node;
@implementation
//init, etc..
But I have seen others use:
@property (nonatomic, etc..) CCNode* node;
@synthesise (nonatomic, etc..) node;
//I am not too sure on how this type of declaration works, please correct me on how it's done.
They both seem to work in the same way, what are the advantages of the @property and @synthesise way? Do they do different things, if so, what?
@property
and @synthesize
are two objective C keyword that allow you to easily create your properties and therefore avoid to write by hand getters and setters methods of the property.
The @property
define the property itself, should be placed in the header file and can get some attributes (as for example : strong, nonatomic, retain assign, copy), the @synthesize
should be placed into the implementation file and tell the compiler to generate the body of getter and setter method.
These two keyword are extremely useful when coupled with the right use of their attributes, because they take care of the generation of the property code and most of all they take care of the memory management of the property.
@property
- create the declaration of your getter and setter.
@synthesize
- provide the definition of getter and setter based upon the parameters which are passed inside property.
Check this out, there are a lot more details about the same present there - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
on using @property
the compiler will take care of declaring getter and setter methods based on readonly and readwrite
readonly -> getterMethod
readwrite ->
both setter and getter method
on using @synthesize
the compiler will take care of defining getter
and setter
methods
If you have an instance variable (ivar) in your class, you can't access it from other classes usually. So you have to make public accessor methods (getters and setters). They look something like this:
Setter:
- (void)setMyVariable:(SomeClass *)newValue {
if (newValue != myVariable) {
[myVariable release];
myVariable = [newValue retain];
}
}
Getter:
- (SomeClass *)myVariable {
return myVariable;
}
This was the way you had to do it before Objective-C 2.0. Now you can use @property and @synthesize to speed this up. It's basically just a shortcut.
In the header you use @property to define what kind of setters you want. Should the setter retain the passed value (like in my example) or copy or just assign?
And in the implementation you just write @synthesize to make the compiler include the automatically created getters and setters at that position. Usually at the top of your implementation.
My feeling is that all iVars should have an associated underscore synthesised property (using an _iVar prevents accidental direct access), and all access to the iVars, apart from init and dealloc methods, should via the property.
IMHO the big win is memory management - it's safer and much easier as there is no need to remember which iVars have been retained.
And think of how much work is required to code an accessor - 4 lines for getter and 2 for a setter.
At some point in the future @synthesize
is likely to be optional, so all you'll need is the @property
.
来源:https://stackoverflow.com/questions/10799015/use-of-property-and-synthesise