views:

803

answers:

1

I was just wondering if this is possible... if I have a "Static class" (a class with a bunch of static methods) is it possible to have a class variable and access it through one of the static methods?

I am getting a warning of "instance variable accessed in class method". I maybe just not getting it. Is there anyone that can answer this question.

Thanks

Johnny_G

+3  A: 

You can use static variables to implement the equivalent of class variables:

// Foo.h
@interface Foo : NSObject {
}
+ (NSObject*)classVariable;
@end

// Foo.m
#import "Foo.h"

static NSObject* classVariable;

@implementation Foo 
+ (NSObject*)classVariable {
  return classVariable;
}
@end
Nathan de Vries
Basically correct, but you need to initialise any member variables that you want to use this way, either by writing accessors to them or by using +(void)initialise on this class, it gets called by the runtime before the program starts
Harald Scheirich
Cool, thanks alot I guess I was just being dumb ;)