I learned today that when writing your own getter/setter methods, ivars aren't automatically synthesized in the getter: property not working with getter AND setter
To get around this, I just set my property to be nonatomic
and then only wrote the setter-- which allowed me to avoid writing @synthesize property = _property
I know the main purpose for nonatomic
properties is so the system can read/write the property faster because it doesn't concern itself with locking the property while writing
But as a side-effect (as it seems), you don't have to write both a setter and getter.
Is this bad? I'm just not sure if there are other drawbacks I'm not seeing
EDIT: Adding code for clarity:
Implementation:
@interface GalleryManager ()
{
NSObject<GalleryManagerDelegateProtocol>* _delegate;
}
...
-(NSObject<GalleryManagerDelegateProtocol>*)delegate
{
return _delegate;
}
-(void)setDelegate:(NSObject<GalleryManagerDelegateProtocol> *)delegate
{
[self stopObservingDelegate];
_delegate = delegate;
[self startObservingDelegate];
if ( delegate && self.didWakeFromNib )
{
[self setupDefaults];
[self readGalleryFromDisk];
}
}
Header:
@property (weak, nonatomic) NSObject<GalleryManagerDelegateProtocol> *delegate;
I wanted to remove the instance variable defined in the extension (_delegate
), for no reason other than I didn't think it was necessary
But by doing so, the -delegate
getter method broke, unable to recognize _delegate
. Because I had the @property
in the header, I thought the _delegate
ivar would be automatically synthesized, but as I have found it is only recognized in the setter.
So in that SO question I linked at the beginning, the answer was to add @synthesize delegate = delegate
at the beginning of the implementation
But instead I wanted to just change the @property
in the header to be nonatomic
, and then just straight up delete the -delegate
getter method
Again, for no other reason than to be as clean as possible