我观察的变化rate
,通过调用像这样在它下面的方法的AVPlayer的性质:
addObserver:sharedPlayerManagerInstance
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
context:0];
该change
的字典,我回去从observeValueForKeyPath:ofObject:change:context:
因此:
change = { kind: 1,
new: 1,
old: 0 }
我检查了每类值,它原来是__NSCFNumber。 然而,当我尝试转换[change objectForKey:@"new"]
或[change objectForKey:@"old"]
为一个int,NSInteger的,的NSNumber,甚至试过的NSString等,它适用于“老”,但它给我的“新”分析错误:
error: expected a type
error: 1 errors parsing expression
我真的需要对它们进行比较,我也搞不清楚。 难道这是iOS中的错误吗?
目标C
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSNumber * newValue = [change objectForKey:NSKeyValueChangeNewKey];
NSInteger integerValue = newValue.integerValue;
int intValue = newValue.intValue;
}
迅速
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if let newValue = change[NSKeyValueChangeNewKey] as? NSNumber{
let intvalue = newValue.intValue //To int
}
}
斯威夫特3:
if let newValue = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
// Do stuff
}
文章来源: How can I get int values from the change dictionary in KVO method observeValueForKeyPath:ofObject:change:context:?