Reputation: 3274
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?
Upvotes: 3
Views: 3760
Reputation: 130172
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]
.
Upvotes: 9
Reputation: 6112
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.
Upvotes: 4
Reputation: 885
@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.
Upvotes: 2