如果任何受監視的對象屬性被修改,是否可以添加觀察者以獲得通知?例如:整個對象屬性的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