Dee*_*rma 3 key-value-observing avfoundation avplayer swift swift4
我observe在使用Swift 4中的新API 时遇到了问题.
player = AVPlayer()
player?.observe(\.currentItem.status, options: [.new], changeHandler: { [weak self] (player, newValue) in
if let status = AVPlayer.Status(rawValue: (newValue as! NSNumber).intValue) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误
如果没有更多的上下文,表达的类型是不明确的.
我如何解决它?不确定keyPath语法.
在上面的闭包中提取AVPlayerStatus时也有一个警告
从'NSKeyValueObservedChange'转换为不相关的类型'NSNumber'总是失败"
currentItem是一个可选属性AVPlayer.以下编译在Swift 4.2/Xcode 10中(注意键路径中的附加问号):
let observer = player.observe(\.currentItem?.status, options: [.new]) {
(player, change) in
guard let optStatus = change.newValue else {
return // No new value provided by observer
}
if let status = optStatus {
// `status` is the new status, type is `AVPlayerItem.Status`
} else {
// New status is `nil`
}
}
Run Code Online (Sandbox Code Playgroud)
observe属性是可选的AVPlayer.Status?,因此
change.newValue回调内部是"双可选" AVPlayer.Status??,必须解包两次.
它可能无法在较旧的Swift版本中编译,比较 Swift的'observe()'对于带有选项的关键路径不起作用?在Swift论坛.
| 归档时间: |
|
| 查看次数: |
964 次 |
| 最近记录: |