Re: Global in NSApplication
Re: Global in NSApplication
- Subject: Re: Global in NSApplication
- From: Jens Alfke <email@hidden>
- Date: Wed, 27 Jan 2010 09:29:45 -0800
On Jan 27, 2010, at 8:37 AM, Scott Ribe wrote:
Well, even simpler, you could just use C without fancy singletons,
as in:
Debug *gDebug = nil;
int main(int argc, char *argv[])
{
gDebug = [[Debug alloc] init];
return NSApplicationMain(argc, (const char **) argv);
}
Depending on what gDebug does, this could be unsafe. It would be a bad
idea to make any AppKit calls before NSApplicationMain, for example.
This also makes it difficult to use the Debug class in a framework/
library.
It can also be tempting to initialize gDebug in something like the
Debug class's +initialize method; but this is still dangerous because
something might access gDebug before calling any class methods on
Debug. (The use of a +load method would fix this, but that's
discouraged because it slows down launch.) I had an issue like this in
an app I wrote once, and we kept running into bugs where some code
happened to access gMe before it had been initialized. In hindsight I
really should have just made it a +sharedInstance method instead of a
global variable.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden