3

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?

akuritsu
  • 440
  • 1
  • 4
  • 18

5 Answers5

5

@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

rishi
  • 11,779
  • 4
  • 40
  • 59
5

@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.

aleroot
  • 71,077
  • 30
  • 176
  • 213
2

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

ajacian81
  • 7,419
  • 9
  • 51
  • 64
anonymous
  • 21
  • 1
0

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.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Thanks, I understand now what they are for, but are the advantages of using them instead of the first example in the question? – akuritsu May 30 '12 at 08:01
0

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.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 1
    Not "likely", definitely will be. The optional `@synthesize` is already on the road map for a future release of Clang/LLVM. It may even be in the next Xcode. – JeremyP May 29 '12 at 15:39