4

I want to create a subclass of NSOperation that needs to customize -isReady, the getter for a KVO-compliant property. My override would do a Boolean-AND of my custom test and super's version of the method. But the override still has to keep KVO-compliance. So, how?

CTMacUser
  • 1,996
  • 1
  • 16
  • 27

1 Answers1

1

Nothing too special, as synthesized properties are KVC and KVO compliant:

The property:

@property (nonatomic, readwrite, getter=isReady) BOOL ready;`

The implementation:

@synthesize ready;

- (BOOL) isReady {
   // your custom logic here.
}

For an NSOperation subclass, KVO notifications for those properties will be automatic. You do not need to do anything more (you do not need to call will/didSetValueForKey). If the behavior of the NSOperation set accessors such as isReady or isFinished is dependent on other properties or key paths, be sure to register them with KVO:

+ (NSSet *) keyPathsForValuesAffectingIsFinished {
    NSSet   *result = [NSSet setWithObject:@"finished"];
    return result;
}
quellish
  • 21,123
  • 4
  • 76
  • 83
  • Important for my code is that the implementation of `isReady` incorporates the superclass' version. There are KVO notifications when either version changes, which is perfect for me because listeners can't tell which version broadcasted. (I'll be in trouble if I had to suppress the superclass' KVO notifications.) – CTMacUser Sep 16 '14 at 04:13
  • I've been meaning to answer and close the query myself, but I procrastinated. Good thing for you since I'll probably give you credit. – CTMacUser Sep 16 '14 at 04:19