title: Global Variables for Class Methods
body: <h3>Background</h3>
  
  <p>In Cocoa, Apple frequently makes use of the following paradigm:</p>
  
  <pre><code>[NSApplication sharedApplication]
  [NSNotificationCenter defaultNotificationCenter]
  [NSGraphicsContext currentContext]
  [NSCalendar currentCalendar]
  </code></pre>
  
  <p>and so on.</p>
  
  <p>They also will occasionally make use of a paradigm that I feel is <strong>far</strong> more legible when working with vast amounts of code.</p>
  
  <pre><code>NSApp //which maps to [NSApplication sharedApplication]
  </code></pre>
  
  <h3>Goal</h3>
  
  <p>I'd love to be able to utilize this sort of global variable, both in my own classes, and in extensions to other classes.</p>
  
  <pre><code>MYClassInstance
  NSDefaultNotificationCenter
  NSCal /* or */ NSCurrentCalendar
  </code></pre>
  
  <p>and so on.</p>
  
  <h3>The "duh" Approach</h3>
  
  <p><code>#define</code>. Simply <code>#define NSCal [NSCalendar currentCalendar]</code>, but as we all know by now, macros are evil (or so they say), and it just doesn't seem like the right Cocoa way to go about this.</p>
  
  <h3>Apple's Approach</h3>
  
  <p>The only source I could find regarding <code>NSApp</code> was <code>APPKIT_EXTERN id NSApp;</code>, which is not exactly reusable code. Unless I'm mistaken, all this code does is define <code>NSApp</code> to be an <code>id</code> the world around. Unfortunately unhelpful.</p>
  
  <h3>Close, but not Quite</h3>
  
  <p>In my searches, I've managed to find several leads regarding "global constants", however things like this:</p>
  
  <pre><code>extern NSString * const StringConstant;
  </code></pre>
  
  <p>are unfortunately limited to compile-time constants, and cannot map to the necessary class method.</p>
  
  <h3>Bottom Line</h3>
  
  <p>I'd love to be able to roll my own <code>NSApp</code>-style global variables, which map to class methods like <code>[NSNotificationCenter defaultNotificationCenter]</code>. Is this possible? If so, how should I go about it?</p>
  
  <h3>Further Attempts</h3>
  
  <p>I'm trying to implement specifically the framework singletons in the following way:</p>
  
  <p>MySingletons.h</p>
  
  <pre><code>//...
  extern id NSNotifCenter;
  //...
  </code></pre>
  
  <p>MySingletons.m</p>
  
  <pre><code>//...
  +(void)initialize
  {
      NSNotifCenter = [NSNotificationCenter defaultCenter];
  }
  //...
  </code></pre>
  
  <p>MyAppDelegate.m</p>
  
  <pre><code>//...
  #import "MySingletons.h"
  //...
  //in applicationDidFinishLaunching:
      [MySingletons initialize];
      NSLog(@"%@", NSNotifCenter);
  //...
  </code></pre>
  
  <p>However, this results in a compile-time error where the _NSNotifCenter symbol cannot be found.</p>
  
  <h3>Goal!</h3>
  
  <p>I'm currently working on an Objective-C class to encapsulate some of the framework singletons I've referred to in this question. I'll add the GitHub information here when I get it up.</p>