NSUnknownKeyException : was sent to an object that is not KVC-compliant for the “player” property

白昼怎懂夜的黑 提交于 2020-04-12 10:41:20

问题


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)

And the log is

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.'


回答1:


Swift does not have the Key value observer of its own so for Swift 4 we have to add:

@objc dynamic

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!



来源:https://stackoverflow.com/questions/46419893/nsunknownkeyexception-was-sent-to-an-object-that-is-not-kvc-compliant-for-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!