0

NSApplication.shared "returns the application instance, creating it if it doesn’t exist yet."

Why is current application instance named 'shared'?

[The similarly named NSWorkspace.shared makes sense to me as it is the workspace shared by apps running in the current NSWorkspace]

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anshuman Sharma
  • 417
  • 2
  • 11
  • It's a singleton. Is your question about the phrase "creating it if it doesn’t exist yet."? – Grimxn Oct 19 '17 at 19:14
  • This is less of a programming question and more of a naming question. – JAL Oct 19 '17 at 19:16
  • Now that I've read Luca's response below, my curiosity about the seemingly odd choice of name is satiated. As a developer not familiar with Apple's convention of using 'shared' to refer to instantiation using the singleton pattern, the name made no sense. Man, people are quick to downvote stuff on this site! – Anshuman Sharma Oct 19 '17 at 19:29
  • Possible duplicate of [Cocoa singleton and shared instances](https://stackoverflow.com/questions/38474619/cocoa-singleton-and-shared-instances) – Tunvir Rahman Tusher Oct 19 '17 at 20:17
  • "Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit your question." I've already got the answer I want. The question may be deleted or archived or done-whatever-with that satisfies the requirements of stack overflow's standards. – Anshuman Sharma Oct 19 '17 at 20:23

1 Answers1

1

In Cocoa in the past the Singleton pattern used a static property named sharedInstance to reference the only instance available for a given singleton class.

Every piece of code that needed an instance of the Singleton class (like NSApplication) could get it using the sharedInstance property.

So that instance was actually shared among different parts of the app.

In 2016 the naming conventions for the APIs of macOS, iOS, tvOS and watchOS has been modernised and sharedInstance has been renamed simply shared.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148