i have a singleton class with a 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"];
}
i “observe” the changes made to this array from another class
my 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]);
}
this works and i am notified when there is an object removed or added
however i cannot figure out (and maybe its not possible) how to see the object that was removed or added (mainly need the object when its removed not added though)
i know the NSDictionary variable Change should have my object but all it has is one key “kind” which always equals 1 since there is always 1 change made to the array.
the nsmutablearray is filled with numbers 500,100,300
so when one of those numbers is removed i would like to know which number was removed in my observer class
how can i do that?
answer code:
[CCV addObserver:self forKeyPath:@"heldJoins" options: NSKeyValueObservingOptionOld ||NSKeyValueChangeNewKey context:NULL];
NSLog(@"Received notification: keyPath:%@ ofObject:%@ change:%@",keyPath, object, [change valueForKey:@"new"]);
It sounds like you didn’t specify NSKeyValueObservingOptionOld when you added the observer.