Re: Instance methods VS. Class methods
Re: Instance methods VS. Class methods
- Subject: Re: Instance methods VS. Class methods
- From: Mike Shields <email@hidden>
- Date: Sat, 24 Nov 2001 02:07:14 -0700
On Saturday, November 24, 2001, at 12:51 AM, Andreas Monitzer wrote:
The following is valid code (but results in a warning for whatever
reason):
@interface MyObject: NSObject {
int value;
}
+ (void)initialize;
@end
@implementation MyObject
+ (void)initialize {
value=10;
}
@end
It's giving a warning because you are trying to access an instance
variable from a class method. If you were wanting something similar to
C++'s class variables, then you would do something like this:
@interface MyObject: NSObject {
}
+ (void)initialize;
@end
@implementation MyObject
static int value;
+ (void)initialize {
value=10;
}
@end
In this case it's not quite the same since the static int value; is just
a plain old C static variable (limited to scope in this compilation
unit), but for most purposes, accomplishes the same thing.
Mike