Search code examples
swiftavkit

Why does AVPlayerView not have a member named "player" in Swift


I was trying to use AVPlayerView in Swift, but the compiler complaints that it cannot find a member named player in AVPlayerView.

Error saying AVPlayerView does not have a member named "player"

But AVPlayerView clearly does have a property called player.

AVPlayerView class definition shows it has a property named "player"

And other properties or methods are fine. It seems that only player is not recognized.


Solution

  • You explicitly have to import the AVFoundation framework (which defines the AVPlayer class):

    import AVKit
    import AVFoundation
    
    let playerView = AVPlayerView()
    let player = playerView.player
    

    That might be considered as a bug in the (Swift view of the) AVKit framework.