NSUnknownKeyException:被发送到与“播放器"不兼容的KVC的对象.财产 [英] NSUnknownKeyException : was sent to an object that is not KVC-compliant for the "player" property
问题描述
我已将代码更新为在Xcode 9中快速运行4.在此之前,它可以正常工作.但是现在AVplayer崩溃了,下面是崩溃的代码.
I have updated my code to swift 4 in Xcode 9. Before that it was working fine. But now AVplayer is crashing at observers below is the code where it is crashing.
addObserver(self, forKeyPath: "player.currentItem.duration", options: [.new, .initial], context: &playerViewControllerKVOContext)
日志是
由于未捕获的异常而终止应用程序 'NSUnknownKeyException',原因:'[addObserver:forKeyPath:@"player.currentItem.duration"选项:5 context:0x10ff74ac8]已发送到不符合KVC的对象 玩家"属性."
Terminating app due to uncaught exception 'NSUnknownKeyException',reason: '[ addObserver: forKeyPath:@"player.currentItem.duration" options:5 context:0x10ff74ac8] was sent to an object that is not KVC-compliant for the "player" property.'
推荐答案
Swift没有自己的键值观察器,因此对于Swift 4,我们必须添加:
Swift does not have the Key value observer of its own so for Swift 4 we have to add:
@objc dynamic
位于需要观察其值的属性之前.在您的情况下,它将是AVPlayer实例.例如:
before the property whose value you need to observe. In your case it will be the AVPlayer instance. e.g.:
class MyPlayerCustomView: UIView {
@objc dynamic var myPlayer: AVPlayer?
}
希望这可以解决您的问题.它确实解决了我的问题!
Hope this solves your problem. It did solve mine!
这篇关于NSUnknownKeyException:被发送到与“播放器"不兼容的KVC的对象.财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!