问题
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!
回答1:
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.
回答2:
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).
回答3:
I hope this will help you.
@property and @synthesize is Use to Access Object Or Variable into Another Class.
Here is a Small Example: This is First Class
#import <UIKit/UIKit.h>
#import "ClassB.h"
@interface ViewController : UIViewController
@property(nonatomic, retain) NSString *FirstName;
@property(nonatomic, retain) NSString *LastName;
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize FirstName, LastName;
- (void)viewDidLoad
{
[super viewDidLoad];
self.FirstName = @"Ashvin";
self.LastName = @"Ajadiya";
ClassB *ClassBOb = [[ClassB alloc] init];
ClassBOb.ViewCntrlrOb = self;
[ClassBOb CallMe];
}
@end
And This is Another Class:
#import <UIKit/UIKit.h>
@class ViewController;
@interface ClassB : UIViewController
@property(nonatomic, retain) ViewController *ViewCntrlrOb;
-(void) CallMe;
@end
#import "ClassB.h"
#import "ViewController.h"
@interface ClassB ()
@end
@implementation ClassB
@synthesize ViewCntrlrOb;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void) CallMe
{
NSLog(@"FirstName = %@",ViewCntrlrOb.FirstName);
NSLog(@"LastName = %@",ViewCntrlrOb.LastName);
}
So You can Access FirstName And LastName into ClassB.
And They Print:
2012-05-25 14:38:10.766 MyExample[8751:c07] FirstName = Ashvin 2012-05-25 14:38:10.768 MyExample[8751:c07] LastName = Ajadiya
回答4:
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.
来源:https://stackoverflow.com/questions/3169822/what-is-the-point-of-property-and-synthesize