Does Objective-C support class variables?

Voloda2 picture Voloda2 · Jun 25, 2012 · Viewed 20.2k times · Source

I know it supports automatic variables, but what about class variables?

Answer

Sergey Kalinichenko picture Sergey Kalinichenko · Jun 25, 2012

The language does not support class variables. You implement class-specific state with global static variables in the compilation units of the implementation.

In the header (.h file):

@interface MyClass : NSObject
+(int)val;
@end

In the implementation (.m file):

static int val = 123;

@implementation MyClass
+(int)val {return val;}
@end

Usage:

if ([MyClass val] > 100) ...