Re: Simple newbie problem: Static variables in Objective-C?
Re: Simple newbie problem: Static variables in Objective-C?
- Subject: Re: Simple newbie problem: Static variables in Objective-C?
- From: "David W. Halliday" <email@hidden>
- Date: Fri, 22 Mar 2002 13:37:26 -0600
- Organization: TNRCC
Andrew Baldwin wrote:
>
I know that it is possible to have static class variables in
>
Objective-C, ...
(Unfortunately) Objective-C does not have "class variables" (static, or
otherwise). However, class variables can be /simulated/ by means of static
variables declared within the file that declares or implements a class. (The
primary problem this has in in inheritance---since there are no true class
variables, such cannot, truly, be inherited.)
>
... but I have searched the documentation for the last hour and
>
tried multiple ways of declaring a static variable and none has
>
satisfied the compiler. All I want is something like the following:
>
>
@interface UniqueIDGenerator : NSObject
>
{
>
static unsigned long long msgID;
>
}
>
@end
Static declarations follow the usual C rules---it's simply a C variable
declaration, nothing more or less (it's just being used in the context of
Objective-C).
Simply move the static declaration outside the instance variable "region" (the
curly brackets, above). (It may be necessary to move it outside the interface or
implementation body. I'm not certain, and don't have a compiler on this system.)
>
In + (void)initialize I would then set msgID to 0. How do I declare
>
msgID? I can't build because I get "Parse error before static".
>
>
I know I should have been able to solve this easily on my own, but I
>
read the Objective-C reference in the developer documentation and even
>
when I basically copied the example code (using NSNumber* instead of
>
Sibling*) I couldn't get it to compile and would keep getting a parse
>
error at static, or when I tried to use the static NSNumber* (created in
>
initialize) I would get a compile error that it was undeclared.
The "Object-Oriented Programming and the Objective-C Language" document has no
examples of static variables within the instance variable "region" of a class
interface or implementation. (In fact, the only example code I find that uses the
"static" keyword is within a method body---so it's local to that method [again,
usual C rules apply].)
>
Thanks for any help,
>
Andrew
>
...
email@hidden
_______________________________________________
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.