This is ridiculous, I just made this proposal on another matter.
You simply expose the variable that contains the singleton instance as the most global. NSApp does not actually display a sharedApplication call. This is a regular old pointer; it was configured during the application launch process to point to the same instance that you would receive from this call.
Like NSApp , you declare a variable for any file that imports the header:
extern MySingleton * MySingletonInstance;
in the header (you can use APPKIT_EXTERN if you want: the docs indicate that it just solves extern in ObjC anyway).
In the implementation file, you define a variable. Typically, a variable containing a shared instance is declared static to limit its association with this file. If you remove static , the statement defines the repository that is βupdatedβ in the header.
Then use it as before. The only caveat is that you still have to call the oneton [MySingleton sharedInstance] installation method before using the WAN for the first time to make sure it is initialized. -applicationDidFinishLaunching: may be a good candidate for a place to do this.
Regarding creating pointers to frame singlets, you can simply write the result of [CocoaSingleton sharedInstance] in any variable you like: ivar in the class that wants to use it, a local variable or in a global variable that you initialize very early in your program with using the function you are writing.
The fact is that this is not guaranteed so as not to cause problems. Except in the case of NSApp (or if it is not documented NSApp ), there really is no guarantee that the object that you return from any given sharedInstance call will remain alive, valid, or useful after the call stack ends,
It may just be paranoia, but I would advise against it if you cannot find a guarantee somewhere that the alleged singletones that interest you always return the same instance. Otherwise, you may suddenly encounter a dangling global pointer.
Turning to your code, the header declaration does not create a variable. You still need a definition:
// MySingletons.h // Dear compiler, There exists a variable, NSNotifCenter, whose // storage is elsewhere. I want to use that variable in this file. extern id NSNotifCenter;
// MySingletons.m // Dear compiler, please create this variable, reserving memory // as necessary. id NSNotifCenter; @implementation MySingletons // Now use the variable. // etc.
If you are creating a singleton, you can take a look at the Apple singleton documentation .