When you need to initialize a static variable in Java, you can do something like that:
public class MyClass {
private static Object someStaticObject;
static {
// initialize someStaticObject here
}
...
How can you do the same in Cocoa?
Specifically, here is what I am after: I have an app with a large number of user preferences. I would like to manage all these preferences from one class where all methods are static, as follows:
@implementation Preferences
+(void)setMotion:(BOOL)isMotion {
[[NSUserDefaults standardUserDefaults] setBool:isMotion forKey:keyIsMotion];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(BOOL)isMotion {
[[NSUserDefaults standardUserDefaults] boolForKey:keyIsMotion];
}
So that I can access and set my preference easily anywhere in my code with:
[Preferences setMotion:TRUE];
or
if ([Preferences isMotion]) {
...
Given that I plan to have tens of static methods, it would like to have a static variable defaults defined as follows:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
so that my code above could become:
+(void)setMotion:(BOOL)isMotion {
[defaults setBool:isMotion forKey:keyIsMotion];
[defaults synchronize];
}
+(BOOL)isMotion {
[defaults boolForKey:keyIsMotion];
}
However, I am not sure how to accomplish that.