如果任何受监视的对象属性被修改,是否可以添加观察者以获得通知?例如:整个对象属性的KVO
@interface OtherObject : NSObject
@property (nonatomic) MyObject* myObject;
@end
和
@interface MyObject : NSObject
@property (nonatomic) unsigned int property1;
@property (nonatomic) unsigned int property2;
@end
我想这样做:
[otherObject addObserver:self
forKeyPath:@"myObject"
options:0
context:nil]
如果任一property1或property2被修改得到通知。如果我注册了保持对象,它似乎不起作用(某种程度上是有道理的,因为myObject在修改property1时没有真正修改)。
假设你也可以结合这两种方法,使用Objective-C运行时获得keyPathsForValuesAffectingMasterProperty中所有属性的列表,然后在NSSet中返回它们。 (使用静态变量可能是个好主意,所以你只需要做一次。) – dgatwood