Monitoring insertions and deletions from NSMutableSet using the observValueForKeyPath function

I would like to be notified of a new insert in NSMutableSet , and thus this is what I am doing, but for some reason it does not call the observeValueForKeyPath method

Just for the test:

 -(void)observ{ [self addObserver:self forKeyPath:@"connections" options:NSKeyValueChangeInsertion context:NULL]; [connections addObject:@"connectionName"]; } 

This is never called:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if( [keyPath isEqualToString:@"connections"] ) { NSLog(@"added new object"); } } 

Is NSMutablSet KVC?

+4
source share
1 answer

NSMutableSet really meets the requirements of KVO / KVC. However, in order to receive notifications with how you configured this, you need to implement KVC access methods for dialing. Information can be found here . Essentially, you must implement methods called:

 -countOfConnections -enumeratorOfConnections -memberOfConnections: -addConnectionsObject: -removeConnectionsObject: -intersectConnections: 

You must use these methods to access and modify your set in order to receive KVO notifications.

Finally, in your -observeValueForKeyPath method -observeValueForKeyPath you can use the value of the kind key in the change dictionary to determine what type of mutation has occurred (add, delete, etc.). Values ​​can be found here and are listed in the NSKeyValueChange section. Hope this helps.

+3
source

Source: https://habr.com/ru/post/1384568/


All Articles