I want to ovveride getter for lazy instantiation and leave default setter.
Do I need @synthesize ?
Why ?
@interface Foo()
@property (strong, nonatomic) NSObject *bar;
@end
@implementation Foo
- (NSObject *)bar
{
if(!_bar) _bar = [[NSObject alloc] init];
return _bar;
}
@end
Update: I've changed variable and class name, because it was confusing. From Deck and card to Foo and bar.
No, you only need to explicitly synthesize (to get the synthesized ivar) if you explicitly implement all of the accessor methods (both getter and setter for
readwrite
properties, just the getter forreadonly
properties). You've written the getter for thisreadwrite
property, but not the setter, so the ivar will still be synthesized for you. Thus, as your code stands, you do not need to explicitly@synthesize
.If you made this property
readonly
, then implementing a getter would prevent your ivar from being automatically synthesized. Likewise, since this isreadwrite
, if you implemented both the getter and the setter, that would require you to synthesize the ivar (if you wanted one).