• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: proper syntax for static variables in a class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >proper syntax for static variables in a class (From: Donald Hall <email@hidden>)

  • Prev by Date: Re: About the accessibility stuff...
  • Next by Date: Registering for NSNotification during notification post
  • Previous by thread: proper syntax for static variables in a class
  • Next by thread: Re: proper syntax for static variables in a class
  • Index(es):
    • Date
    • Thread