Does Objective-C support class variables?

ObjC class variables are plain old static variables.

Foo.m:

static int foo = 0;

Alternatively, you can use a C++ anonymous namespace if you use ObjC++:

Foo.mm:

namespace {
    int foo = 0;
}

But there is another pattern if you want to benefit from the advantages of properties:

Foo.h:

@interface FooShared

@property ( atomic, readwrite, strong ) Foo* foo;

@end

@interface Foo

+ (FooShared*) shared;

@end

Foo.m:

@implementation FooShared
@end

static fooShared* = nil;

@implementation Foo

+ (FooShared*) shared
{
    if ( fooShared == nil ) fooShared = [FooShared new];

    return fooShared;
}

@end

somewhere.m:

Foo* foo …;
foo.shared.foo = …;

It may look a bit overkill, but it's a interesting solution. You use the same constructs and language features for both instance properties and "class" properties. Atomicity on demand, accessors when needed, debugging, breakpoints... Inheritance even.

Creative minds can find other ways to do all this I suppose. :) But you're pretty much covered with these options.


Class properties were added back in Xcode 8.x. It's not much more than if you wrote your own setters and getters as class methods, and used statics for storage. In fact, that's all that it is, except for the compiler messages, see below.

Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as @property (class) NSString *someStringProperty;, and are never synthesized. (23891898)

https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

@interface MyClass

@property (class) NSObject *someClassProperty;
@property (class, readonly) NSString *aReadClassProperty;

@end

If you don't add the required getters/setter, you'll see these errors:

Class property 'aReadClassProperty' requires method 'aReadClassProperty' to be defined - use @dynamic or provide a method implementation in this class implementation

Class property 'someClassProperty' requires method 'setSomeClassProperty:' to be defined - use @dynamic or provide a method implementation in this class implementation

Class property 'someClassProperty' requires method 'someClassProperty' to be defined - use @dynamic or provide a method implementation in this class implementation

in your implementation, you'll need:

@implementation MyClass

static NSObject *object;

+ (void)setSomeClassProperty:(NSObject *)someClassProperty {
    object = someClassProperty;
}

+ (NSObject *)someClassProperty {
    return object;
}

static NSString *string = @"My class string";

+ (NSString *)aReadClassProperty {
    return string;
}

@property (class, nonatomic, copy) NSString *someStringProperty;

But you have to provide getter and setter

From the Xcode 8 release notes: Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as: @property (class) NSString *someStringProperty;. They are never synthesized. (23891898)


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) ...