I want to observe a NSMutableArray in my class cSoundChannel. Hence after reading this post Observing an NSMutableArray for insertion/removal I implemented the key observing in this manner.
For cSoundChanel class,
My property for the mutable array is
@property (assign, nonatomic) NSMutableArray* midiDevices;
The functions I introduced using kvo array accessors in the class are as follows :
- (void) addmidiDevicesObject:(NSObject *) str {
[self insertObject:str inMidiDevicesAtIndex:[_midiDevices count]];
}
- (void)insertObject:(NSObject *)str inMidiDevicesAtIndex:(NSUInteger)index {
[self.midiDevices insertObject:str atIndex:index];
return;
}
For my ViewController.m file, where I need to observe midiDevices, I did the following.
[self.cSoundChannel addObserver:self forKeyPath:@"midiDevices" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
and expected to be able to observe the mutable array in ...
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"midiDevices"]) {
NSLog(@"Let's see!");
}
}
but alas... it did not print "Let's see!" Observing other things... NSString etc works...
Is there anything that I missed out? Help!