0

I have a private BOOL in a class

@property (nonatomic, assign) BOOL myBool;

I want to override the getter and setter so I can either synchronize it, or dispatch it to a serial queue.

- (BOOL)myBool
{
    __block BOOL test = NO;
    dispatch_sync(self.shouldShowBannerQueue, ^{
        test = _myBool; //This does not work. I get an error "Use of undeclared _myBool"
    });

    return test;
}

I cannot override the setter for the same reason. I cannot directly access _myBool.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
chris P
  • 6,359
  • 11
  • 40
  • 84

1 Answers1

0

If you are defining the accessor methods, the implicit synthesis will not happen and you won't have the _myBool variable. But you can simply add an instance variable to the class and use it to back the accessor methods.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • 2
    Or add `@synthesize myBool;` – Borys Verebskyi Nov 15 '16 at 18:06
  • @BorisVerebsky Or `@synthesize myBool = _myBool;` to be equivalent to the implicit behaviour. Personally I prefer to add the ivar if I'm writing custom accessors, since I feel the `@synthesize` hints that they would have default behaviour. – Arkku Nov 15 '16 at 18:11