views:

14

answers:

1

I have three different properties, x, y, z. If they all turn nil, I need to take an action, and if one of them is set to a value != nil, I have to carry out a different action.

My current implementation is the following:

@property (readonly) NSNumber *meta;

+ (NSSet *)keyPathsForValuesAffectingMeta
{
  return [NSSet setWithObjects:@"x", @"y", @"z", nil];
}

- (void)awakeFromNib
{
  [self addObserver:self forKeyPath:@"meta" options:0 context:NULL];
}

- (NSNumber *)meta
{
  BOOL meta = x || y || z;
  return [NSNumber numberWithBool:meta];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  BOOL meta = [[self meta] boolValue];
  if (meta != _flags.meta) {
    if (meta) {
      [self showSomeStuff];
    }
    else {
      [self hideSomeStuff];
    }

  _flags.meta = meta;
  }
}

It works, but I hope there is a much easier and better solution for something like this, that I am currently overlooking. Hit me!

+1  A: 

I think how you have it is about the cleanest and most Cocoa-like way you could. And for the record, observing self is not a bad design decision.

Dave DeLong