Objective c 关于KVO和方法的问题;observeValueForKeyPath“;
您能否帮助我理解observeValueForKeyPath方法: 这里调用observeValueForKeyPath似乎是因为我们更改了“键:earthquakeList”的值,但假设我们观察到了另一个键,如“地震新列表” 如果我们只有一个回调方法observeValueForKeyPath,我怎么知道观察到的第一个或第二个键已经改变了Objective c 关于KVO和方法的问题;observeValueForKeyPath“;,objective-c,key-value-observing,Objective C,Key Value Observing,您能否帮助我理解observeValueForKeyPath方法: 这里调用observeValueForKeyPath似乎是因为我们更改了“键:earthquakeList”的值,但假设我们观察到了另一个键,如“地震新列表” 如果我们只有一个回调方法observeValueForKeyPath,我怎么知道观察到的第一个或第二个键已经改变了 [self addObserver:self forKeyPath:@"earthquakeList" options:0 context:NULL]; /
[self addObserver:self forKeyPath:@"earthquakeList" options:0 context:NULL];
//...
- (void)insertEarthquakes:(NSArray *)earthquakes
{
// this will allow us as an observer to notified (see observeValueForKeyPath)
// so we can update our UITableView
//
[self willChangeValueForKey:@"earthquakeList"];
[self.earthquakeList addObjectsFromArray:earthquakes];
[self didChangeValueForKey:@"earthquakeList"];
}
// listen for changes to the earthquake list coming from our app delegate.
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
[self.tableView reloadData];
}
感谢在您的
observeValueForKeyPath:ofObject:change:context:
实现中的keyPath
参数将告诉您哪个键已更改。因此,您可以执行以下操作:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"key1"]) {
// do something
} else if ([keyPath isEqualToString:@"key2"]) {
// do something else
}
}