我编写了一个程序,当视频播放器出现在屏幕上时收到通知并从中获取信息。该代码在 Swift 2 上可以运行,但现在在 Swift 3.0 上却无法运行。它说“ Assets 使用不明确”。看我的代码:
func VideoPlayedInFullscreen(_ aNotification: Notification) {
let player = aNotification.object
if player == nil {
return
}
//this line below shows ambiguous use of asset error
let ast = (player as! AnyObject).asset
}
我会非常感谢你的。请帮忙
最佳答案
编译器——就像我一样——不知道 player
是什么类型。通过将其转换为实际类型来告诉编译器该类型。
我想 - 在查看文档之后 - 它是 AVPlayerItem
所以代码应该是:
func VideoPlayedInFullscreen(_ aNotification: Notification) {
guard let player = aNotification.object as? AVPlayerItem else { return }
let ast = player.asset
}
在 Swift 中,使用 if - let
或 guard - let
而不是简单地检查 nil
。好处是成功后您可以解开对象。
关于ios - Swift 3 中 "asset"的使用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820609/