如何在 Cocoa 中进行静态初始化
当你需要在 Java 中初始化静态变量时,你可以这样做:
public class MyClass {
private static Object someStaticObject;
static {
// initialize someStaticObject here
}
...
How can you do the same in Cocoa?
具体来说,这就是我所追求的:我有一个具有大量用户偏好的应用程序。我想从一个类中管理所有这些首选项,其中所有方法都是静态的,如下所示:
@implementation Preferences
+(void)setMotion:(BOOL)isMotion {
[[NSUserDefaults standardUserDefaults] setBool:isMotion forKey:keyIsMotion];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(BOOL)isMotion {
[[NSUserDefaults standardUserDefaults] boolForKey:keyIsMotion];
}
这样我就可以在代码中的任何位置轻松访问和设置我的首选项:
[Preferences setMotion:TRUE];
或者
if ([Preferences isMotion]) {
...
鉴于我计划拥有数十个静态方法,它将喜欢有一个静态变量默认值定义如下:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
这样我上面的代码就可以变成:
+(void)setMotion:(BOOL)isMotion {
[defaults setBool:isMotion forKey:keyIsMotion];
[defaults synchronize];
}
+(BOOL)isMotion {
[defaults boolForKey:keyIsMotion];
}
但是,我不知道如何实现这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 Objective-C 对象上重写
+ (void)initialize
方法。来自 NSObject< 上的 Apple 文档/a>:
运行时在程序中的每个类或任何继承自该类的类从程序内发送其第一条消息之前,恰好向该类发送初始化一次。 (因此,如果不使用该类,则该方法可能永远不会被调用。)运行时以线程安全的方式将初始化消息发送到类。超类在其子类之前收到此消息。
您可以使用该方法来初始化静态 ivars 和/或 NSUserDefaults
You can override
+ (void)initialize
method on your Objective-C object.From Apple Docs on NSObject:
The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.
You can used that method it initialize static ivars and or NSUserDefaults
简而言之,只需在类的实现文件的实现块中声明静态变量(但在任何方法之外)。然后为静态变量提供访问器方法,就像上面提到的那样。
阅读 Objective-C 的类变量 另请参阅此帖子
In short, just declare the static variable in the implementation block of your class's implementation file (but outside of any method). Then provide accessor methods to the static variable, just as you mentioned above.
Read Class Variables for Objective-C and see also this post