How do I declare class-level properties in Objective-C?
properties have a specific meaning in Objective-C, but I think you mean something that's equivalent to a static variable? E.g. only one instance for all types of Foo?
To declare class functions in Objective-C you use the + prefix instead of - so your implementation would look something like:
// Foo.h
@interface Foo {
}
+ (NSDictionary *)dictionary;
// Foo.m
+ (NSDictionary *)dictionary {
static NSDictionary *fooDict = nil;
if (fooDict == nil) {
// create dict
}
return fooDict;
}
I'm using this solution:
@interface Model
+ (int) value;
+ (void) setValue:(int)val;
@end
@implementation Model
static int value;
+ (int) value
{ @synchronized(self) { return value; } }
+ (void) setValue:(int)val
{ @synchronized(self) { value = val; } }
@end
And i found it extremely useful as a replacement of Singleton pattern.
To use it, simply access your data with dot notation:
Model.value = 1;
NSLog(@"%d = value", Model.value);