Re: Global in NSApplication
Re: Global in NSApplication
- Subject: Re: Global in NSApplication
- From: Scott Ribe <email@hidden>
- Date: Wed, 27 Jan 2010 09:37:20 -0700
- Thread-topic: Global in NSApplication
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);
}
Then in any file where you want to use it, put "extern Debug *gDebug;". Or
even just include that line at the end of Debug.h.
I toss this out just to remind you that Objective-C includes C, so you
shouldn't forget C techniques. Or, alternatively, if you don't really know
C, you should try to find some time to learn it. For instance, Objective-C
includes no specific way to declare a global variable simply because C
already does so.
Of course this code does not necessarily *prevent* you from making multiple
Debug instances, but neither does the code Graham posted if you try to call
[Debug sharedDebug] from multiple threads. To really prevent it, if
necessary, for a shared library perhaps, takes a bit more work. But if it's
your own class, it might be good enough to just have a "don't do that"
attitude, (or a check in Debug -init that throws an exception) ;-)
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
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