Next: Setting a delegate for Up: The shared application object Previous: The shared application object

Creating the shared NSApplication instance

The class NSApplication, provided by the GNUstep GUI library, represents a gui application. Each gui application has one (and only one) instance of this class, which is shared by all the code; it keeps tracks of all the application windows and panels, and manages the application's run loop. You access this shared instance by calling the +sharedApplication method of NSApplication, which creates the instance of NSApplication representing your app the first time it is invoked, and returns the previously created instance when called again. Creating the shared application is very important, because when it is first created the gui library initializes the gnustep backend; in other words, you need to create the shared application object before doing anything at all with the gui or backend (xgps/xdps) library. So, we will start our first gui application with the code:
NSApplication *myApplication;

myApplication = [NSApplication sharedApplication];
An interesting thing to know is that, after you have created an NSApplication shared instance for your app, you can access it simply through the global variable NSApp. So, many people simply discard the result of +sharedApplication, and start their apps as follows:
/* The following line creates the shared application instance */
[NSApplication sharedApplication];

/* Then, use NSApp to access NSApplication's shared instance  */


Next: Setting a delegate for Up: The shared application object Previous: The shared application object
Nicola Pero 2000-07-21