2011-10-31 3 views
1

j'ai une classe singleton avec un nsmutablearrayKVO NSMutableArray supprimer notification objet

- (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"]; 
} 

i "observer" les modifications apportées à ce tableau d'une autre classe mon 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]); 
    } 

cela fonctionne et i Je suis averti quand il y a un objet supprimé ou ajouté Cependant, je ne peux pas comprendre (et peut-être ce n'est pas possible) comment voir l'objet qui a été enlevé ou ajouté (principalement besoin de l'objet quand il est enlevé gh)

je connais la NSDictionary variable Change devrait avoir mon objet mais tout ce qu'il a est une clé "genre" qui est toujours égale à 1 car il y a toujours 1 changement apporté au tableau.

le nsmutablearray est rempli de numéros 500100300 donc quand l'un de ces numéros est enlevé je voudrais savoir quel numéro a été retiré dans ma classe d'observateurs comment puis-je faire?

code de réponse:

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

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

Répondre

2

On dirait que vous n'avez pas spécifié NSKeyValueObservingOptionOld lorsque vous avez ajouté l'observateur.

+0

merci, mal ajouter le code approprié au fond –

+0

Votre appel addObserver utilise les constantes incorrect pour les options. Par exemple vous utilisez NSKeyValueChangeOldKey mais la constante correcte est (comme je l'ai dit dans ma réponse) NSKeyValueObservingOptionOld. –

+1

Vous utilisez également '||' mais vous devriez utiliser '|'. –

Questions connexes