2011-10-31 62 views
1

我有一个nsmutablearray志愿的NSMutableArray删除对象通知

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    NSLog(@"Received notification: keyPath:%@ ofObject:%@ change:%@", 
      keyPath, object, change); 

    //[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 

} 
//array accessors 
- (void)insertNewObject:(id)object 
{ 
    [self willChangeValueForKey:@"heldJoins"]; 
    [heldJoins addObject:object]; 
    [self didChangeValueForKey:@"heldJoins"]; 
} 
- (void)removeObject:(id)object 
{ 
    [self willChangeValueForKey:@"heldJoins"]; 
    [heldJoins removeObject:object]; 
    [self didChangeValueForKey:@"heldJoins"]; 
} 

一个单独的类我“观察”从另一个类 我RootViewController的这个阵列中所做的更改

[CCV addObserver:self forKeyPath:@"heldJoins" options:NSKeyValueChangeOldKey||NSKeyValueChangeNewKey context:NULL]; 

}  
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
    {  
     NSLog(@"Received notification: keyPath:%@ ofObject:%@ change:%@",keyPath, object, [change allKeys]); 
    } 

这工作,我当有一个对象被删除或添加通知 但是我无法弄清楚(也许它不可能)如何看到被删除或添加的对象(主要需要该对象时,其删除不添加你gh)

我知道NSDictionary变量Change应该有我的对象,但它只有一个键“kind”,它始终等于1,因为总是会对该数组进行一次更改。

nsmutablearray充满号500100300 所以当这些数字被删除我想知道在我的观察类中删除该号码 我该怎么办呢?

答案代码:

[CCV addObserver:self forKeyPath:@"heldJoins" options: NSKeyValueObservingOptionOld ||NSKeyValueChangeNewKey context:NULL]; 

NSLog(@"Received notification: keyPath:%@ ofObject:%@ change:%@",keyPath, object, [change valueForKey:@"new"]); 

回答

2

这听起来像你当你添加的观察者没有指定NSKeyValueObservingOptionOld。

+0

谢谢,虐待添加正确的代码底部 –

+0

您的addObserver调用使用错误的常量的选项。例如,你正在使用NSKeyValueChangeOldKey,但正确的常量是(正如我在我的答案中所说)NSKeyValueObservingOptionOld。 –

+1

你也在使用'||',但你应该使用'|'。 –