What is the meaning of @property and @synthesize?

I could not figure it out, and there are no websites that explain it clearly enough ... What are the goals of @property and @synthesize ?

Thanks in advance!

+5
iphone ios4
source share
4 answers

Objective-C Runtime Programming Guide: Declared Properties

@property declares getter and setter methods for the public property that you want to implement. For example, this property declaration:

 @property float value; 

equivalent to:

 - (float)value; - (void)setValue:(float)newValue; 

@synthesize provides a default implementation for these two accessories.

Update : The above explains what the two are doing. This does not explain what their purpose is. :-)

  • @property adds a member to the public interface, which acts as a data variable for your class clients, but is read and written using methods. This gives you better control over the data exchanged between the client and your code, for example, you can perform an extended check on the values ​​provided by your code.
  • @synthesize allows you to explicitly not write code that will be called by the client, and actually treat the property as a data variable on its own.
+13
source share

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 instruct the compiler to create instructions and corresponding characters for getters and setters for you. Remember that in C, the operator "=" means "assign". In most cases, in the OO context provided by the Objective-C extensions, we work with pointers (also references) to isa data structures (classes in Objective-C).

Prior to Objective-C 2.0, all getter and setter methods had to be coded by the developer for each attribute, which in most cases was copy / paste code. To be fully compatible with KVC / KVO, it takes a lot of very tedious code ... willAccessValueForKey, doneUpdateValueForKey, etc., which the new compiler automatically adds for you when using the @ property / @ synhesize syntax. This is a huge productivity boost for developers. Adding point syntax to the language is a bit more controversial in the community, as it hides the magic that the compiler does from you to interpret the operator object.property = anotherObject.property; like [object setProperty:[anotherObject property]];

From Apple documentation referenced in other answers

Property Declaration Attributes

You can decorate a property with attributes using the @property form (attribute [, attribute2, ...]). Like methods, properties are tied to an interface declaration. For property declarations that use a comma-separated list of variable names, property attributes apply to all named properties.

If you use the @synthesize directive to inform the compiler of the creation of the access method (s), the code that it generates matches the specification given by the keywords. If you implement the access method yourself, you must make sure that it conforms to the specification (for example, if you specify a copy, you must make sure to copy the input value in the setter method).

+3
source share

Hope this helps you.

@property and @synthesize is used to access an object or variable in 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 the first and last names in ClassB.

And they type:

2012-05-25 14: 38: 10.766 MyExample [8751: c07] FirstName = Ashvin 2012-05-25 14: 38: 10.768 MyExample [8751: c07] LastName = Ajadiya

+3
source share

Just a quick example of why you might not want just "variable = 0":

Say you have this property:

 @property (nonatomic, retain) id <MyDelegate> theDelegate; 

Whenever you replace a delegate with a new one, your synthesized setters and recipients will handle the release / save for you every time you set it like this:

 self.theDelegate = newObject; 

In fact, the following happened:

 [self setTheDelegate:newObject]; - (void)setTheDelegate:(id <MyDelegate>)anObject { [theDelegate release]; theDelegate = [anObject retain]; } 

(This, of course, is simplified)

You can do very powerful things in your setters and getters, synthesize for those that repeat over and over, like stored properties, etc. When compiling, it looks at your @property and builds the methods accordingly.

0
source share

All Articles