• 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: Global variables?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global variables?


  • Subject: Re: Global variables?
  • From: Uli Kusterer <email@hidden>
  • Date: Wed, 28 Dec 2005 00:24:19 +0100


Am 27.12.2005 um 15:50 schrieb Daniel Jalkut:
If you want to take the "quick and dirty" route for this, it has nothing to do with Objective-C or OOP. Just use a global variable in your main controller's source file, as you would in any conventional C setting:

int myArray[10];

Then make an extern reference to it in the associated header file:

extern int myArray[10];

Now everybody who imports the header file has access to the array.

Just do yourself and your friends a favor and name the variable gMyArray or follow some other standard that makes clear this is a global.

I'd personally would combine two suggestions made: Declare the global as a static in your source file and write class-level accessor methods for it through which all others will go. That way, when you change the size of the array or whatever, you can easily change central code in the accessor and track down bugs more easily as they all go through the accessors. I.e.:


#define MY_ARRAY_SIZE 10

static int sMyArray[MY_ARRAY_SIZE];

@implementation ArrayWrapperClass

+(int) getIntAtIndex: (unsigned)idx
{
if( idx >= MY_ARRAY_SIZE || idx < 0 )
[NSException raise... ];
return sMyArray[idx];
}

+(void) setInt: (int)val AtIndex: (unsigned)idx
{
if( idx >= MY_ARRAY_SIZE || idx < 0 )
[NSException raise... ];
sMyArray[idx] = val;
}

@end

Cheers,
-- M. Uli Kusterer
http://www.zathras.de


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
References: 
 >Global variables? (From: Matt Reagan <email@hidden>)
 >Re: Global variables? (From: Daniel Jalkut <email@hidden>)

  • Prev by Date: Re: Agent Applications and launchd
  • Next by Date: Re: Prevent running in Rosetta
  • Previous by thread: Re: Global variables?
  • Next by thread: Prevent running in Rosetta
  • Index(es):
    • Date
    • Thread