gpt4 book ai didi

ios - 如何从 KVO 方法 observeValueForKeyPath :ofObject:change:context:? 中的更改字典中获取 int 值

转载 作者:行者123 更新时间:2023-12-01 18:10:56 26 4
gpt4 key购买 nike

我通过调用以下方法来观察 AVPlayer 的 rate 属性的变化:

addObserver:sharedPlayerManagerInstance
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
context:0];

因此,我从 observeValueForKeyPath:ofObject:change:context: 返回的 change 字典是:

change = { kind: 1,
new: 1,
old: 0 }

我查看了每个值的类,原来是__NSCFNumber。但是,当我尝试将 [change objectForKey:@"new"][change objectForKey:@"old"] 转换为 int、NSInteger、NSNumber 时,甚至尝试过NSString 等,它适用于“旧”,但它给了我一个"new"的解析错误:

error: expected a type
error: 1 errors parsing expression

我真的需要比较它们,我想不通。这可能是 iOS 中的错误吗?

最佳答案

objective-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;
}

swift

   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
}
}

关于ios - 如何从 KVO 方法 observeValueForKeyPath :ofObject:change:context:? 中的更改字典中获取 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com