5

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!

esqew
  • 42,425
  • 27
  • 92
  • 132

4 Answers4

13

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.
Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • I don't know if I fully understood this... why not just do: value = 0.0; – esqew Jul 03 '10 at 00:51
  • @seanny94 See this question: http://stackoverflow.com/questions/1568091/why-use-getters-and-setters – Tobias Cohen Jul 03 '10 at 03:18
  • 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 Jul 03 '10 at 12:32
  • 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 Jul 03 '10 at 19:00
  • @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 Jul 04 '10 at 10:00
  • 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 Jul 04 '10 at 14:12
  • 1
    Link is dead (and comments must be over 15 characters) – Rog Jul 30 '15 at 09:23
  • Updated with more up to date link. (Hopefully, Apple will learn the concept of permalink one of these days.) – Franci Penov Jul 31 '15 at 01:05
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

Ashvin
  • 8,227
  • 3
  • 36
  • 53
3

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
  • 4,170
  • 1
  • 21
  • 22
0

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,576
  • 14
  • 19