super allocWithZone having some doubts in singleton class concept

前端 未结 3 653
南旧
南旧 2021-02-06 08:25

I am new in Objective-C and I am trying to create a singleton class based on Apple\'s documentation.

+ (MyGizmoClass*)sharedManager
{
    if (sharedGizmoManager          


        
3条回答
  •  长发绾君心
    2021-02-06 08:57

    The other answers, though they point out good information with regard to singletons, didn't actually answer your question. Your question is actually mostly based on Object orientation, the fact that you specifically reference a singleton is incidental.

    1. I answered this question with reference to self, here is the paraphrased, important part of the answer

      super does have meaning in class level contexts, but it refers to the superclass itself, not an instance

    2. This one was throwing me off too. I asked this question and it was concluded:

      [super class] calls the super method on the current instance (i.e. self). If self had an overridden version, then it would be called and it would look different. Since you don't override it, calling [self class] is the same as calling [super class].

    3. Are you sure it's actually returning an instance of this class? Or are you assigning it to an instance sharedGizmoManager of this class?

    4. Super isn't equal to self, but some of the methods you have called: e.g. [super class] is calling the same implementation of the method that [self class] would call.

提交回复
热议问题