Johnny_Generic
Johnny_Generic

Reputation:

Static classes with iPhone

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?

Upvotes: 1

Views: 2567

Answers (1)

Nathan de Vries
Nathan de Vries

Reputation: 15511

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

Upvotes: 5

Related Questions