从singleton类调用方法时的空变量

时间:2022-10-01 20:48:01

So I have a singleton class in which I have implemented the delegate of my module. However when that delegate method gets executed in this singleton I call a method in another class of the App and it crashes because all the variables previously set in that class are empty.

所以我有一个单例类,我已经实现了我的模块的委托。但是当在这个单例中执行该委托方法时,我在App的另一个类中调用一个方法,它崩溃了,因为之前在该类中设置的所有变量都是空的。

class Player: Jukebox, JukeboxDelegate {

    static let sharedInstance = Player()

    func setDelegate(){
        jukebox = Jukebox(delegate: self)
    }

    func play(Link: String) {
        jukebox.setIT([JukeboxItem(URL: NSURL(string: Link)!)])
        jukebox.setImage(currentImage)
        jukebox?.play()
    }

    func audioPlayerDidFinishPlaying(player: Jukebox, successfully flag: Bool) {
        if pltype == PlayerType.Playlist {
            MyMusicVC.continuePlaying() { () -> () in
            }

        }
    }

    func jukeboxStateDidChange(state: Jukebox) {

    }


    func jukeboxPlaybackProgressDidChange(jukebox: Jukebox) {

    }

    func jukeboxDidLoadItem(jukebox: Jukebox, item: JukeboxItem) {

    }

    func jukeboxDidUpdateMetadata(jukebox: Jukebox, forItem: JukeboxItem) {

    }
}

Any ideas on why could this be happening?

关于为什么会发生这种情况的任何想法?

1 个解决方案

#1


2  

Its hard to understand what is going on. But you are subclassing a Jukebox class. Then making this subclass a delegate of the Jukebox itself. Then I also notice you're setting a Jukebox property that appears to be optional but not.

很难理解发生了什么。但是你是一个Jukebox类的子类。然后使这个子类成为Jukebox本身的委托。然后我还注意到你正在设置一个看似可选但不是的Jukebox属性。

For example. Why in the play() function is jukebox. used twice but the last line is jukebox?. Is jukebox optional? Or implicitly unwrapped? And if thats the case, why are you using it unwrapped twice and then optionally the third time.

例如。为什么play()函数是jukebox。使用了两次,但最后一行是点唱机?点唱机是可选的吗?或隐含地解开?如果是这样的话,你为什么要使用它解开两次,然后可选择第三次。

Have you tried not making this singleton a subclass of jukebox. Instead just make it conform to the JukeBoxDelegate protocol and see if that accomplishes the same goal?

您是否尝试过不将此单例作为自动点唱机的子类。相反,只需使它符合JukeBoxDelegate协议,看看是否实现了相同的目标?

#1


2  

Its hard to understand what is going on. But you are subclassing a Jukebox class. Then making this subclass a delegate of the Jukebox itself. Then I also notice you're setting a Jukebox property that appears to be optional but not.

很难理解发生了什么。但是你是一个Jukebox类的子类。然后使这个子类成为Jukebox本身的委托。然后我还注意到你正在设置一个看似可选但不是的Jukebox属性。

For example. Why in the play() function is jukebox. used twice but the last line is jukebox?. Is jukebox optional? Or implicitly unwrapped? And if thats the case, why are you using it unwrapped twice and then optionally the third time.

例如。为什么play()函数是jukebox。使用了两次,但最后一行是点唱机?点唱机是可选的吗?或隐含地解开?如果是这样的话,你为什么要使用它解开两次,然后可选择第三次。

Have you tried not making this singleton a subclass of jukebox. Instead just make it conform to the JukeBoxDelegate protocol and see if that accomplishes the same goal?

您是否尝试过不将此单例作为自动点唱机的子类。相反,只需使它符合JukeBoxDelegate协议,看看是否实现了相同的目标?