1

Say I have an NSWindowController subclass: MyWindowController.
For this class, I have a singleton instance, sharedWindowController

Inside of my implementation of MyWindowController, within my methods should I be referencing self or [MyWindowController sharedWindowController]?

In a normal subclass the answer would be self; but I am looking at some legacy code in my codebase, and the previous author has been referencing [MyWindowController sharedWindowController]. I'm assuming this is because in theory there will only ever be one instance of MyWindowController, so by referencing sharedWindowController, we are just being safe?

But is this unnecessary?

A O
  • 5,516
  • 3
  • 33
  • 68
  • are you talking about instance methods or class methods? – Amin Negm-Awad Apr 06 '16 at 19:40
  • I think it's more prudent to refer to `self` within that class, because then the code isn't dependent upon its being a singleton. You'd generally only use `sharedWindowController` if referencing this from somewhere where you can't otherwise know to which instance you are referring, e.g. from other classes or from a class method within `MyWindowController`. (As an aside, using a singleton for a window controller seems a bit suspect to me, but that's a broader question.) – Rob Apr 06 '16 at 21:07

2 Answers2

2

It is both unnecessary and bad.

in theory there only ever will be one

From the description you have a shared instance model, not a singleton model, there could be more than one. Debugging would get messy quickly due to unexpected object interactions.

And while not significant the code is also larger and slower.

So the code takes a risk, introduces potential bugs, and all for no gain.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • "From the description you have a shared instance model, not a singleton model, " I would give +432923 for this, because most people (including those on SO and parts of the Apple documentation) does not know the difference. – Amin Negm-Awad Apr 06 '16 at 19:43
  • I'm currently reading up on shared instance vs singleton, will report back – A O Apr 06 '16 at 20:16
  • (I know this is unrelated to your answer) Alright so unfortunately after reading a few articles, I couldn't find an example of the Singleton pattern in objective-c that wasn't using what appears to be the SharedInstance pattern? As you said, the difference appears to be that there can be more than one instance in the SharedInstance model. But it is actually possible to truly implement a singleton pattern in objective-C? – A O Apr 06 '16 at 20:27
  • By "truly implement", I mean truly guarantee there will only ever be one instance of a class? – A O Apr 06 '16 at 20:28
  • @AO - Apple used to publish code for true singletons, there are others. To show I'm completely unbiased see [this SO answer](http://stackoverflow.com/questions/30799696/singleton-in-ios-objective-c-doesnt-prevent-more-than-one-instance/30828622#30828622) ;-) – CRD Apr 06 '16 at 23:04
  • @AP *… I couldn't find an example …* – yes, this is, what I said: Most people think that this two patterns are identically including parts of the actual Apple documentation. They are wrong, even they are in the majority. – Amin Negm-Awad Apr 07 '16 at 16:02
0

Either there is only one, then [SomeClass sharedThingy] and self refer to the same object, but self is much faster and much less code to write.

Or there is more than one, then self is still faster and much less code to write, but it refers to the right object.

In each case, better to refer to self.

On top of that, [SomeClass sharedThingy] will create a deadlock on the first call with some bad luck (if anything referred to in the allocation code uses [SomeClass sharedThingy]).

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Theoretically – and I emphasize that – theoretically it is possible in a singleton pattern that the instance changes after a previous instance is deallocated. It only says that there is a single instance at a specific time. But of course, this is why I wrote theoretically, this should never happen while an instance method is running. (Okay, maybe because of another thread, but this is still theoretically and many things will break in such a situation.) – Amin Negm-Awad Apr 07 '16 at 15:59