How to check Key Value Observing in ObjectiveC for dictionary of arrays?

百般思念 提交于 2019-12-11 13:42:07

问题


I have the following code:

 @property (strong, nonatomic) NSMutableDictionary *todoSessionDict;
    @synthesize todoSessionDict;

[self addObserver:self forKeyPath:@"todoSessionDict" options:NSKeyValueObservingOptionNew context:NULL];


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

}

------------------------
NSMutableArray *unCompletedTodosArr = [NSMutableArray  array];
NSMutableArray *completedTodosArr   = [NSMutableArray  array];

 [self.todoSessionDict setObject:unCompletedTodosArr forKey:@"unCompletedTodosArr"];
 [self.todoSessionDict setObject:completedTodosArr forKey:@"completedTodosArr"];

Any idea how to check if the @"unCompletedTodosArr" and @"completedTodosArr" values are changed in the self.todoSessionDict using KVO ?


回答1:


At first, It should be:

 [self addObserver:self forKeyPath:@"todoSessionDict.unCompletedTodosArr" options:0 context:nil];
  [self addObserver:self forKeyPath:@"todoSessionDict.completedTodosArr" options:0 context:nil];

And for both, the methods:

  #pragma mark - KVO
   -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
   {
     // Here your actions depends on the key path...
    }

    -(void)didChangeValueForKey:(NSString *)key

     {

     }


来源:https://stackoverflow.com/questions/27482173/how-to-check-key-value-observing-in-objectivec-for-dictionary-of-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!