3

When I first learned about iOS programming (I believe this was from the pre-ARC Stanford lectures), we always synthesized properties like this:

@synthesize myProperty=_myProperty

However, it appears that this has been the default behaviour for a while. So, omitting this statement is the exact same as leaving it in.

My question is this: given this default behaviour, what are some practical examples where we'd actually want to use a @synthesize statement?

Rogare
  • 3,234
  • 3
  • 27
  • 50

3 Answers3

9

With iOS 6, the LLVM compiler came with something called "property autosynthesis". Basically, that means that the compiler will add a @synthesize for every @property it sees in the following form:

@synthesize propertyName = _propertyName;

that is, it will create an ivar prefixed with underscore and it will generate the getter & setter using this ivar.

There is only one reason to use @synthesize explicitly and the reason is to rename the ivar or, for example, use the same ivar for two different properties.

Also note that in some cases you need to use @dynamic to prevent the synthesis. Typically when you know the methods (getter or setter) are already defined somewhere but the compiler doesn't know about them.

There are no memory implications with autosynthesis so it is not connected to ARC at all.

What if I manually implement getter and a setter?

If the property is readwrite and you implement both setter and a getter or if the property is readonly and you implement the getter, then nothing will be synthesized. The synthesis refers to the automatic definition of the two methods. Why should the compiler synthesize something that is already there? If the manual implementation needs an ivar, just declare the ivar, you don't need to add @synthesize just to add an ivar (although you can).

EDIT

I forgot one reason when using @synthesize is needed and that is when implementing a property declared in a protocol. Such a property won't be autosynthesized. A typical example is [UIApplicationDelegate window].

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Thanks for the reply, very clear and to the point. One thing I'm still fuzzy on is whether synthesis is needed if you implement both the setter and the getter for a property? – Rogare Apr 29 '15 at 10:59
  • @Rogare It's not. I expected you know what property synthesis does so I tried to explain only the auto synthesis. – Sulthan Apr 29 '15 at 11:10
  • OK, no problem. This requirement was mentioned in a different answer: "If you implement both the setter and getter though you need to also manually include synthesize" which I was interested in your comment on. – Rogare Apr 29 '15 at 11:27
  • 1
    @Rogare Added an explanation for the last question. Explicit synthesis is used for that sometimes but it's not needed. It also depends on the use case, most computed properties don't even need an ivar. – Sulthan Apr 29 '15 at 11:43
4

You don't have to use this. Simply declare your public properties like this in your .h :

@property (nonatomic, strong/weak/assign) MyType *propertyName;

More details about ARC here

Additional note :
@synthesize was used to link a property with an internal class variable. It's done automaticaly with ARC.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
2

@synthesize in objective-c just implements property setters and getters:

- (void)setCoolWord:(NSString *)coolWord {
     _coolWord = coolWord;
}

- (NSString *)coolWord {
    return _coolWord;
}

It is true with Xcode 4 that this is implemented for you (iOS6 requires Xcode 4). Technically it implements @synthesize coolWord = _coolWord (_coolWord is the instance variable and coolWord is the property).

To access these properties use self.coolWord both for setting self.coolWord = @"YEAH!"; and getting NSLog(@"%@", self.coolWord);

Also note, both the setter and getter can still be manually implemented. If you implement BOTH the setter and getter though you NEED to also manually include @synthesize coolWord = _coolWord;

With automatic synthesis in iOS6, it is no longer necessary to specifically declare backing ivars or write the @synthesize statement. When the compiler finds a @property statement, it will do both on our behalf using the guidelines we’ve just reviewed. So all we need to do is declare a property like this:

@property (nonatomic, strong) NSString *abc;

and in iOS 6, @synthesize abc = _abc, will be added automatically at compile time.

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
User1075
  • 819
  • 15
  • 36
  • Thanks for the reply. Am I correct in summarizing this as: "Use @synthesize when you have implemented both a setter and a getter. Otherwise, it has no purpose."? – Rogare Apr 29 '15 at 10:51
  • http://stackoverflow.com/questions/13120927/need-explanation-on-property-synthesize-in-ios – User1075 Apr 29 '15 at 10:56
  • @Abbie There is no connection between (auto)synthesis and ARC. – Sulthan Apr 29 '15 at 10:57
  • If you implement a setter and a getter you don't NEED to add @synthesize, you can alternately explicitly declare the backing iVar. – Jef Apr 29 '15 at 13:01