Swift 如何在不继承 AVAudioPlayer 的 class 上使用 AVAudioPlayerDelegate?

Swift How can I use AVAudioPlayerDelegate on a class that doesn't inherit AVAudioPlayer?

class myClass: AVAudioPlayerDelegate{
    var player = AVAudioPlayer()

    init(){
        player.delegate = self
    }

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
          print("The song ended")
    }
}

我正在学习 Swift 并尝试制作音乐播放应用程序。我有一个自定义 class,它有一个名为 player 的 AVAudioPlayer 对象作为它的 属性。如何将 AVAudioPlayerDelegate 方法与 player 对象一起使用?

当有这样的代码时,我得到错误:

The type myClass does not conform to the protocol NSObjectProtocol

继承自NSObject。

class myClass: NSObject, AVAudioPlayerDelegate {
    var player = AVAudioPlayer()

    init(){
        player.delegate = self
    }

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
          print("The song ended")
    }
}