If by "public static" you mean class properties, you'll be sad to learn that Objective-C doesn't support them. What you do instead is write accessors to set and get the value of a module-wide static variable. Here's what you do:
1. In your header file, declare class methods to use as accessors:
@interface MyClass : NSObject
+ (NSString*)defaultName;
+ (void)setDefaultName:(NSString*)newDefaultName;
@end
2. At the top of your implementation file, declare a static variable to hold the value:
static NSString* MyClassDefaultName = nil;
3. In the implementation section for your class, write your accessors:
@implementation MyClass
+ (NSString*)defaultName {
return MyClassDefaultName;
}
+ (void)setDefaultName:(NSString*)newDefaultName {
if(MyClassDefaultName != newDefaultName) {
[MyClassDefaultName release];
MyClassDefaultName = [newDefaultName retain];
}
}
4. Finally, if you want to set a default value, you can use your class's initialize class method. (initialize may be called more than once, so make sure you check that you haven't already set the variable.)
+ (void)initialize {
if(!MyClassDefaultName) {
MyClassDefaultName = @"Default default name";
}
}
@end
Use [MyClass defaultName] to retrieve the variable, and [MyClass setDefaultName:newValue] to change the variable. Child classes will inherit these methods, but will not be able to set the variable directly; they will have to use the accessors.
Message was edited by: Brent Royal-Gordon
Message was edited by: Brent Royal-Gordon