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!