0

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

Community
  • 1
  • 1
A O
  • 5,516
  • 3
  • 33
  • 68
  • check answer for this question http://stackoverflow.com/questions/588866/whats-the-difference-between-the-atomic-and-nonatomic-attributes – Nik Yekimov Sep 22 '15 at 15:46
  • This isn't clear. How was your property actually declared before this "issue" came up? Why do you start off talking about this issue being caused by implementing both the setter and getter but then later you talk about only doing the setter and not the getter? – rmaddy Sep 22 '15 at 15:51
  • Why are you writing either the setter or the getter here? Do you need one or both? What's wrong with the synthesized setter and/or getter? – rmaddy Sep 22 '15 at 15:52
  • BTW - If you have a declared property and you have a need to explicitly implement both the setter and getter, you don't need the `@synthesize` line. Just declare whatever ivar you might need to implement the methods. – rmaddy Sep 22 '15 at 15:53
  • please clarify, what do you think allowed to avoid writing `@synthesize` and had the side-effect of writing the getter for you? this is perhaps the core of your misunderstanding. – Pierre Houston Sep 22 '15 at 15:54
  • Sorry guys, my fault. I added more info for clarity. Just trying to learn the cleanest way to write code while also not being reckless – A O Sep 22 '15 at 17:19
  • The property is not at risk of a race condition, so it can be `nonatomic` or `atomic` (I believe) – A O Sep 22 '15 at 17:37

0 Answers0