Re: proper syntax for static variables in a class
Re: proper syntax for static variables in a class
- Subject: Re: proper syntax for static variables in a class
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 10 Jun 2002 12:11:02 -0700
On Saturday, June 8, 2002, at 11:41 PM, Donald Hall wrote:
I have an object that I want to be shared by all instances of a
class. Currently I have the following structure, but I wonder
if this is correct. (It seems to compile and run okay, but I
could find nothings in the docs.)
In MyClass.m I have:
#import "MyClass.h"
@implementation MyClass
static NSArray *mySharedData;
+(void)initialize
{
// fill in mySharedData here
}
Declaring mySharedData above '@implementation MyClass' works
equally well.
Declaring mySharedData anywhere in the header file doesn't seem
to work.
Is there a "correct" way to do this? A preferred style?
Your approach is fine, the only thing I would do differently is
defer the creation of the array until it's needed, eg:
#import MyClass.h
static NSArray *mySharedData;
+ sharedData
{
if (!mySharedData)
mySharedData = [[NSArray alloc] initWithWhatever...]
return mySharedData;
}
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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.