• 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: Simple newbie problem: Static variables in Objective-C?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple newbie problem: Static variables in Objective-C?


  • Subject: Re: Simple newbie problem: Static variables in Objective-C?
  • From: Andy Lee <email@hidden>
  • Date: Fri, 22 Mar 2002 14:10:01 -0500

At 1:36 PM -0500 3/22/02, Andrew Baldwin wrote:
I know that it is possible to have static class variables in Objective-C,

Actually, AFAIK you can't. You may be thinking of class variables in C++ or static members in Java.

All I want is something like the following:

@interface UniqueIDGenerator : NSObject
{
static unsigned long long msgID;
}
@end

You can do this the C way, by declaring a static variable in the .m file, and adding getter and setter methods if appropriate:

@implementation UniqueIDGenerator

// ...

static unsigned long msgID = 0;

+ (unsigned long)msgID
{
return msgID;
}

+ (void)setMsgID:(unsigned long)newMsgID
{
msgID = newMsgID;
}

// ...

@end

You could also put the static declaration inside a method, if that's the only method that will access the variable (again, this is just C).

--Andy
_______________________________________________
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: 
 >Simple newbie problem: Static variables in Objective-C? (From: Andrew Baldwin <email@hidden>)

  • Prev by Date: Re: Open Recent menu in non-doc app
  • Next by Date: Re: Does NSSound not work in a Unix tool? - SOLUTION
  • Previous by thread: Simple newbie problem: Static variables in Objective-C?
  • Next by thread: Re: Simple newbie problem: Static variables in Objective-C?
  • Index(es):
    • Date
    • Thread