What's the correct method to subclass a singleton class in Objective -C?

后端 未结 9 966
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 18:23

I have created a singleton class and I want to create a class which is subclass of this singleton class, what is the correct method to do it

9条回答
  •  余生分开走
    2020-12-09 18:34

    If Jon didn't convinced you to not do it, you should do it this way:

    In your superclass, init your singleton instance with [[[self class] alloc] init] so then you always get an instance of the class with which you are calling the sharedInstance method. And you don't have to overwrite the sharedInstance method in your subclass.

     [SuperClass sharedInstance] //-> instance of SuperClass
     [SubClass sharedInstance] //-> instance of Class
    

提交回复
热议问题