【发布时间】:2013-09-13 10:12:37
【问题描述】:
我想在我的班级 cSoundChannel 中观察 NSMutableArray。因此,在阅读了这篇文章后 Observing an NSMutableArray for insertion/removal 我以这种方式实现了关键观察。
对于 cSoundChanel 类,
可变数组的属性是
@property (assign, nonatomic) NSMutableArray* midiDevices;
我在类中使用kvo数组访问器介绍的函数如下:
- (void) addmidiDevicesObject:(NSObject *) str {
[self insertObject:str inMidiDevicesAtIndex:[_midiDevices count]];
}
- (void)insertObject:(NSObject *)str inMidiDevicesAtIndex:(NSUInteger)index {
[self.midiDevices insertObject:str atIndex:index];
return;
}
对于需要观察 midiDevices 的 ViewController.m 文件,我做了以下操作。
[self.cSoundChannel addObserver:self forKeyPath:@"midiDevices" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
并期望能够在 ...
中观察可变数组-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"midiDevices"]) {
NSLog(@"Let's see!");
}
}
但是,唉...它没有打印“让我们看看!” 观察其他东西...... NSString 等工作......
有什么我错过的吗?帮助!
【问题讨论】:
-
你在哪个类中拥有每一部分的代码?最重要的是 -(void)observeValueForKeyPath: 在同一个类中作为观察者在 addObserver:self 中发送。
-
您好,我编辑了课程,谢谢...
标签: ios objective-c arrays nsmutablearray