Re: Globals
Re: Globals
- Subject: Re: Globals
- From: "Alastair J.Houghton" <email@hidden>
- Date: Sat, 25 Oct 2003 16:17:28 +0100
On Saturday, October 25, 2003, at 03:55 pm, Seth Willits wrote:
On Saturday, October 25, 2003, at 07:37 AM, Seth Willits wrote:
This may be one silly question, but how do you create I have a global
variable for a class?
I should probably clarify that a bit. I want to create a global
variable (which is a class) accessible by any class if it includes the
header for the global.
Normally, given a class called SWThing, you'd declare a function
+ (SWThing *)sharedSWThing;
in your class, then in the implementation you'd do something like this:
@implementation SWThing
static SWThing *sharedSWThing;
+ (SWThing *)sharedSWThing
{
if (!sharedSWThing)
sharedSWThing = [[SWThing alloc] init];
return sharedSWThing;
}
...
That way you don't need to use a global variable.
Obviously if your program is multithreaded, you need to avoid a race to
initialise the sharedSWThing variable. One easy way to do this without
having to resort to locking is to explicitly call your method before
the program goes multithreaded.
Kind regards,
Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Re: Globals (From: Seth Willits <email@hidden>) |